Spring 5 Authentication - PullRequest
       1

Spring 5 Authentication

0 голосов
/ 13 сентября 2018

Я тестирую контроль доступа, как показано ниже, но всегда получаю 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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...