0
我已經聲明實現WebSecurityConfigurerAdapter的類。成功登錄後,我將用戶重定向到我在authorizeRequests中聲明的page/main/list,並將「v_main」角色綁定到此請求url。但我聲明如下配置返回我ACCESS_DENIED我已經聲明頁面accessDeniedPageWebSecurityConfigurerAdapter cofiguration不能正確處理請求
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class AppSecurityConfig extends WebSecurityConfigurerAdapter {
protected static Logger log = Logger.getLogger(AppSecurityConfig.class);
@Autowired
private CustomAuthenticationProvider authProvider;
@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
log.info("configAuthentication");
auth.authenticationProvider(authProvider);
}
@Override
public void configure(WebSecurity web) throws Exception {
log.info("configure WebSecurity");
web.ignoring()
.antMatchers("/assets/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
log.info("configure HttpSecurity");
http
.formLogin()
.loginPage("/login")
.permitAll()
.loginProcessingUrl("/login")
.usernameParameter("username").passwordParameter("password")
.defaultSuccessUrl("/main/list")
.failureUrl("/login?fail=1")
.and()
.authorizeRequests()
.antMatchers("/main/**").hasRole("v_role")
.anyRequest().authenticated()
.and()
.logout()
//.logoutUrl("/logout")
.logoutSuccessUrl("/login?logout")
.invalidateHttpSession(true)
.and()
.exceptionHandling()
.accessDeniedPage("/access_denied")
.and()
.csrf();
}
我的回答
我需要的是改變hasRole到hasAuthority什麼。
.antMatchers("/main/**").hasAuthority("v_main")
因爲我設置與SimpleGrantedAuthority
謝謝回覆。我在實現AuthenticationProvider的CustomAuthenticationProvider中設置角色,並在其身份驗證方法中爲用戶設置角色。順便說一句我測試了你的代碼,並且它返回了我正確的角色 – AEMLoviji
does request.isUserInRole(ga.getAuthority()))對於你正在檢查的角色爲true – Mudassar
對不起。我只是測試它返回的是什麼。它返回我設置給用戶的角色。但正如你所說的,UserInRole不會返回true。什麼是問題? – AEMLoviji