【问题标题】:Spring Security with many role具有多种作用的 Spring Security
【发布时间】:2020-07-20 21:38:45
【问题描述】:

我正在使用 Spring Security(@EnableWebSecurity)。当我尝试在 "/invoice""/notification" 端点上使用此令牌进行授权时,我总是收到禁止响应,但如果我不使用 hasRole 属性或仅使用一个角色的令牌,一切都会正常工作。

代币:

{
....
  "exp": 1586366900,
  "iat": 1586348900,
  "authorities": "ROLE_INVOICE,ROLE_NOTIFICATION",
  ....
}

WebSecurityConfigurerAdapter 类:

        @Override
        protected void configure(HttpSecurity http) throws Exception {

            http.csrf().disable() //Disabling CSRF
                    .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                    .and()
                    .exceptionHandling().authenticationEntryPoint((req, rsp, e) -> rsp.sendError(HttpServletResponse.SC_UNAUTHORIZED))
                    .and()
                    .addFilterAfter(new JwtTokenAuthenticationFilter(jwtConfig), UsernamePasswordAuthenticationFilter.class)
                    .authorizeRequests()
                    .antMatchers(HttpMethod.POST, jwtConfig.getUri()).permitAll()
                    .antMatchers("/invoice/**").hasRole("INVOICE")
                    .antMatchers("/notification/**").hasRole("NOTIFICATION")
                    .antMatchers("/test/**")
                    // Any other request must be authenticated
                    .anyRequest().authenticated();
        }

就我而言,我想在不同服务上使用一个令牌进行身份验证。有什么建议吗?

【问题讨论】:

    标签: spring spring-security jwt express-jwt


    【解决方案1】:

    你的权限必须是一个数组:

    ...
    "authorities": ["ROLE_INVOICE", "ROLE_NOTIFICATION],
    ...
    

    【讨论】:

      猜你喜欢
      • 2014-11-02
      • 2015-07-30
      • 1970-01-01
      • 2016-06-30
      • 1970-01-01
      • 2018-04-14
      • 1970-01-01
      • 1970-01-01
      • 2016-07-07
      相关资源
      最近更新 更多