任務是創建一個Web應用程序,該應用程序應該可以從具有不同角色的不同用戶訪問。有些角色可以查看所有頁面,有些只能查看所有頁面。Java Spring Security無法識別的角色
我對SecurityConfig具有以下配置,但它不起作用。
@Configuration
@EnableWebSecurity
@Import(value = { SecurityWebApplicationInitializer.class })
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
DataSource dataSource;
@Autowired
private AuthenticationService authenticationService;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
Md5PasswordEncoder encoder = new Md5PasswordEncoder();
auth.userDetailsService(authenticationService).passwordEncoder(encoder);
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/assets/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/login", "/page1").permitAll()
.antMatchers("/**").access("hasRole('RADMIN')")
.antMatchers("/login", "/page2").access("hasRole('ADMIN')")
.antMatchers("/page2").hasAnyRole("RADMIN", "ADMIN")
.and().formLogin()
.and().exceptionHandling().accessDeniedPage("/403");
}
}
我試圖改變hasAnyRole()
到hasAnyAuthority()
,沒有效果。幾乎每一次我做的改變都不會讓ADMIN登錄(他只看到/ 403或/ 404),或者讓任何人,無論是否授權,都可以看到所有內容。
標準的Spring Security當局'ROLE_'開始,所以這些應該是'ROLE_RADMIN'和'ROLE_ADMIN',除非你自己定義了['AccessDecisionVoter'](http://docs.spring.io/autorepo/docs/spring-security/4.1.0.RELEASE/apidocs/org/springframework/security/access/ AccessDecisionVoter.html)。 – Andreas
試過,沒有區別... – mariobgr
你確實檢查過ROLE_ADMIN已經分配給用戶了,對嗎? – Andreas