0
訪問所有用戶都可以登錄成功,但所有的人都可以打開它使用permitAll()方法只URL。 FOr網址元我已設置角色「RADMIN」,並在該用戶登錄後,他無法打開元或任何其他網址,因爲403錯誤。可以打開的網址只有「登錄」,「註銷」,「家」。Spring Security的403錯誤時,角色JAVA
@Configuration
@ComponentScan("bg.package")
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SpringSecurity extends WebSecurityConfigurerAdapter {
@Autowired
private AuthenticationService authenticationService;
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/assets/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/login", "/home", "/logout").permitAll()
.antMatchers("meta/**").hasAuthority("RADMIN")
.anyRequest().authenticated()
.and().addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class);
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
Md5PasswordEncoder encoder = new Md5PasswordEncoder();
auth.userDetailsService(authenticationService).passwordEncoder(encoder);
}
}
AuthService
@Service
public class AuthenticationService implements UserDetailsService {
@Autowired
private AuthDao authDao;
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
AuthModel authModel = authDao.getUserInfo(email);
GrantedAuthority authority = new SimpleGrantedAuthority(authModel.getRank());
UserDetails userDetails = (UserDetails) new User(authModel.getName(), authModel.getPass(), Arrays.asList(authority));
return userDetails;
}
}
我已經嘗試過,但都沒有工作。 – cool
什麼是'authModel.getRank()'返回?對於我給你的解決方案,如果'authModel.getRank()'返回'ROLE_RADMIN',應該可以工作。如果它返回'RADMIN',他們所寫的代碼應該沒問題(部分用'hasAuthority(「RADMIN」)') 從上面寫的代碼中,我不能說出什麼是錯誤的,試着調試一下, authModel.getRank()'正在返回。 – Sasa
authModel.getRank()返回ROLE_RADMIN。我用硬編碼值測試了它,它工作正確。現在我發現了這個問題,但我不知道如何解決它。這個方法** loadUserByUsername **沒有被調用,我不知道該怎麼做 – cool