首页 > 解决方案 > 面向所有 API 和用户的集中式 spring secuirty

问题描述

我计划将 spring security 用于我的平台作为集中式。

我有我的身份验证服务器,它为我想要实现安全性的授权进行识别,

我想为所有授权建立一个集中式服务器,集中式服务器将在数据库中拥有所有用户、角色、API、操作和上下文。

Apigetway->Authorisation Server 如果授权正确则 APi gateway->其他微服务

但是当我通过 spring security 时,它在每个微服务中只有 mvc 方法和 preAuthorize 方法,如下所示

mvc方法。

@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(WebSecurity web) throws Exception {
        // Spring Security ignores URLs of static resources
        web.ignoring().requestMatchers(
                PathRequest.toStaticResources().atCommonLocations());
    }


@Override
    protected void configure(HttpSecurity http) throws Exception {
        // configure login
        http.formLogin()
                .loginPage("/login")
                .usernameParameter("account")
                .passwordParameter("password")
                .defaultSuccessUrl("/", true)
                .permitAll();
        // configure logout
        http.logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/login")
                .permitAll()
                .invalidateHttpSession(true);
        // configure URL authorization
        http.authorizeRequests()
                .mvcMatchers("/signup").permitAll()
                .mvcMatchers(HttpMethod.GET, "/issues/").hasAuthority("readIssue")
                .mvcMatchers(HttpMethod.GET, "/issue/new").hasAuthority("writeIssue")
                .mvcMatchers(HttpMethod.POST, "/issues/").hasAuthority("writeIssue")
                .mvcMatchers("/users").hasAuthority("manageUser")
                .anyRequest().authenticated();
    }

预授权方法

@GetMapping
    @PreAuthorize("@securityService.hasPrivilege('READ_USERS') OR principal.username == #model.get('email')")
    public String getAccountView(ModelMap model) {
        return "user/account";
    }

是否有集中的方式来处理弹簧安全?

标签: spring-bootspring-security

解决方案


推荐阅读