簡體   English   中英

Spring 具有多種作用的安全性

[英]Spring Security with many role

我正在使用 spring 安全性( @EnableWebSecurity )。 當我嘗試在"/invoice""/notification"端點上使用此令牌進行授權時,我總是收到禁止的響應,但如果我不使用hasRole屬性或僅使用一個角色的令牌,一切都會正常工作。

代幣:

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

WebSecurityConfigurerAdapter class:

        @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();
        }

就我而言,我想在不同的服務上使用一個令牌進行身份驗證。 有什么建議嗎?

您的權限必須是一個數組:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM