【问题标题】:Spring security - How to use role based authentication for different domains?Spring security - 如何为不同的域使用基于角色的身份验证?
【发布时间】:2021-06-01 08:23:20
【问题描述】:

有一个项目在 localhost:8080 上运行 Spring boot 后端,在 localhost:4200(用户网站)和 localhost:4201(管理网站)上运行 2 个前端 Angular 应用程序。我该如何配置 spring 安全性它只允许用户网站中具有角色 - ROLE_USER,ROLE_ADMIN 的用户和角色 -ROLE_ADMIN 的用户可以访问管理网站。

目前两个用户都可以访问两个 wesbite。有没有办法限制某些域而不是限制用户的路径(URL)。

当前配置 -

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{
    
    @Autowired
    Environment env; 

    @Autowired
    UserSecurityService useSecurityService;
    
    private BCryptPasswordEncoder passwordEncoder() {
        return SecurityUtility.passwordEncoder();
    }
    
    private static final String[] PUBLIC_MATHCES= {
            "/css/**",
            "/js/**",
            "/images/**",
            "/book/**",
            "/user/**",
            "/media/**"
    };

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(useSecurityService).passwordEncoder(passwordEncoder());
        
        
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers(PUBLIC_MATHCES).permitAll()
            .anyRequest().authenticated()
            .and();
        http.csrf().disable()
            .cors()
            .and()
            .httpBasic();
        
    }
     
      @Bean
      public HttpSessionIdResolver httpSessionStrategy() {
          return  HeaderHttpSessionIdResolver.xAuthToken();
      }
      
        
    
}

【问题讨论】:

    标签: spring spring-security


    【解决方案1】:

    假设你的所有配置都配置正确,那么你就可以使用角色限制机制,如下示例:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
    
        http
            .authorizeRequests(authorize - > {
                authorize
                .antMatchers("/h2-console/**").permitAll() //do not use in production!
                .antMatchers("/css/**", "/js/**", "/images/**", "/book/**", "/user/**", "/media/**").permitAll()
                .antMatchers("/website/find", "/main*").permitAll()
                .antMatchers(HttpMethod.GET, "/userweb/v1/data/**").permitAll()
                .mvcMatchers(HttpMethod.DELETE, "/userweb/v1/info/**").hasRole("ADMIN")
                .mvcMatchers(HttpMethod.GET, "/userweb/v1/item/{upc}").permitAll()
                .mvcMatchers("/admin/main").hasAnyRole("USER", "ADMIN")
                .mvcMatchers(HttpMethod.GET, "/user/api/v1/normal")
                .hasAnyRole("USER", "ADMIN", "FOO");
            })
            .authorizeRequests()
            .anyRequest().authenticated()
            .and()
            .cors()
            .and()
            .httpBasic()
            .and().csrf().disable();
    }
    

    【讨论】:

    • 这个运气好吗?
    • 不,我想要一个可以阻止特定域而不是路径的配置,(例如,ROLE_USER 应该只能访问 localhost:4200 而不能访问 localhost:4201),我已经知道如何配置特定的路径
    • @Codemaster 你关于多角色限制的问题,反正没关系,因为每个请求都有自己的凭据,管理员!=用户
    • 我的问题是如何对不同的域使用基于角色的限制
    • @Codemaster 我认为您指的是针对不同域的基于角色的限制,不可能将某些用户限制到某些域。更好的方法是从不同域创建不同的路径请求(例如- localhost:4200 将发送请求 /user/login 和 localhost:4201 将发送请求 /admin/login 然后您可以使用 Mahmoud 提供的 confid )
    猜你喜欢
    • 2023-03-20
    • 2014-08-31
    • 2017-08-03
    • 2015-02-06
    • 2017-07-08
    • 1970-01-01
    • 2019-01-27
    • 2016-01-03
    • 2014-07-01
    相关资源
    最近更新 更多