Я тестирую контроль доступа, как показано ниже, но всегда получаю 403. Мой метод user.getRoles () ниже жестко задан для возврата «USER».Я получаю доступ к URL-адресу от "/ user / **".Почему ему не разрешен доступ, если роль совпадает?
@Override
protected void configure(final HttpSecurity http) throws Exception {
logger.debug("configure");
http
.formLogin()
.loginPage("/login.html")
.failureUrl("/login-error.html")
.and()
.logout()
.logoutSuccessUrl("/index.html")
.and()
.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN")
.antMatchers("/user/**").hasRole("USER")
.antMatchers("/shared/**").hasAnyRole("USER","ADMIN")
.and()
.exceptionHandling()
.accessDeniedPage("/403.html");
}
@Override
public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
String adid = authentication.getName();
Object credentials = authentication.getCredentials();
if (!(credentials instanceof String)) {
return null;
}
String password = credentials.toString();
User user = userService.findUserbyADID(adid);
if (user == null) {
throw new BadCredentialsException("Authentication failed for " + adid);
}
List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
logger.debug("Roles = {}", user.getRoles());
grantedAuthorities.add(new SimpleGrantedAuthority(user.getRoles()));
Authentication auth = new
UsernamePasswordAuthenticationToken(adid, password, grantedAuthorities);
return auth;
}