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