ClientDetailsS​​ervice In Memory无法正常工作

时间:2017-09-06 13:17:40

标签: spring authentication oauth spring-security-oauth2

我将OAUTH2版本从2.0.3更改为2.0.14并且Authorization Server无法正常工作。 我收到了来自服务器的消息:

o.s.s.o.provider.endpoint.TokenEndpoint  : Handling error: InsufficientAuthenticationException, There is no client authentication. Try adding an appropriate authentication filter.

你能帮助我并说出什么问题吗?我在数据库中有令牌存储,我想从内存中使用ClientDetailsService,但Spring没有认识到这一点。

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends 
AuthorizationServerConfigurerAdapter implements EnvironmentAware {

private static final String ENV_OAUTH = "authentication.oauth.";
private static final String PROP_CLIENTID = "clientid";
private static final String PROP_SECRET = "secret";
private static final String PROP_TOKEN_VALIDITY_SECONDS = 
"tokenValidityInSeconds";

private RelaxedPropertyResolver propertyResolver;

@Autowired
private DataSource dataSource;

private BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

@Bean
public TokenStore tokenStore() {
    return new JdbcTokenStore(dataSource);
}

@Autowired
private AuthenticationManager authenticationManager;

@Bean
protected AuthorizationCodeServices authorizationCodeServices() {
    return new JdbcAuthorizationCodeServices(dataSource);
}

@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws 
Exception {
    security.passwordEncoder(passwordEncoder);
}

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) 
throws Exception {
endpoints.tokenStore(tokenStore())
.authenticationManager(authenticationManager);
}


@Override
public void configure(ClientDetailsServiceConfigurer clients) throws 
Exception {
clients.inMemory()
.withClient(propertyResolver.getProperty(PROP_CLIENTID)).scopes("read", 
"write").authorities(Authorities.ROLE_ADMIN.name(), 
Authorities.ROLE_USER.name())
            .authorizedGrantTypes("password", 
"refresh_token").secret(propertyResolver.getProperty(PROP_SECRET))
            .accessTokenValiditySeconds(

propertyResolver.getProperty(PROP_TOKEN_VALIDITY_SECONDS, Integer.class, 
1800));

}

@Override
public void setEnvironment(Environment environment) {
    this.propertyResolver = new RelaxedPropertyResolver(environment, 
ENV_OAUTH);
}

}

安全配置

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

@Autowired
private UserDetailsService userDetailsComponent;

@Bean
public PasswordEncoder passwordEncoder() {
    return new StandardPasswordEncoder();
}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws 
Exception {
auth
.userDetailsService(userDetailsComponent)
.passwordEncoder(passwordEncoder());
}

@Override
public void configure(WebSecurity web) throws Exception { 
web.ignoring()
.antMatchers("/register").antMatchers("/console/*")
.antMatchers("/oauth/**");
}

@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
}


}

方法安全配置。

@EnableGlobalMethodSecurity(prePostEnabled = true, 
proxyTargetClass = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration 
{

}

自定义UserDetailsService

@Component("userDetailsComponent")
public class UserDetailsComponent implements UserDetailsService {

private final Logger log = 
LoggerFactory.getLogger(UserDetailsComponent.class);

@Autowired
private UsersRepository usersRepository;

@Override
public UserDetails loadUserByUsername(String login) {
    log.debug("Authenticating {}", login);

    Users userFromDatabase = null;
    if (login.contains("@")) {
        userFromDatabase = usersRepository.findByEmail(login);
    } else {
        userFromDatabase = usersRepository.findByUsername(login);
    }
    if (userFromDatabase == null) {
        throw new UsernameNotFoundException("User " + login + " was not 
found in the database");
    } else if (!userFromDatabase.getActivated()) {
        throw new UserNotActivatedException("User " + login + " is not 
activated");
    }

Collection<GrantedAuthority> grantedAuthorities = new 
ArrayList<GrantedAuthority>();
    for (OauthAuthority authority : userFromDatabase.getOauthAuthorities()) 
{
        GrantedAuthority grantedAuthority = new 
SimpleGrantedAuthority(authority.getAuthority().getRole());
        grantedAuthorities.add(grantedAuthority);
    }

    return new User(userFromDatabase.getUsername(), 
userFromDatabase.getPassword(), grantedAuthorities);
}

}

属性

authentication.oauth.clientid=game
authentication.oauth.secret=secret
authentication.oauth.tokenValidityInSeconds=2000

0 个答案:

没有答案