TokenEndpointAuthenticationFilter

时间:2018-03-08 13:35:01

标签: java spring spring-boot oauth-2.0 spring-security-oauth2

我使用Spring BOOT + OAUTH2并希望在成功登录后保存用户对数据库注册表的访问权限...正在寻找解决方案我遇到过这个类:{{3} }

所以我已经像这样实现了它: 的 CustomTokenEndpointAuthenticationFilter.java

public class CustomTokenEndpointAuthenticationFilter extends TokenEndpointAuthenticationFilter {

public CustomTokenEndpointAuthenticationFilter(AuthenticationManager authenticationManager, OAuth2RequestFactory oAuth2RequestFactory) {

    super(authenticationManager, oAuth2RequestFactory);
}

@Override
protected void onSuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, Authentication authResult) throws IOException {

            /* on successful authentication do stuff here */

}

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
            /* before authentication check for condition if true then process to authenticate */
    if (!condition) {
        throw new AuthenticationServiceException("condition not satisfied");
    }
    super.doFilter(req, res, chain);
}
}

我的 AuthorizationServerConfiguration.java

@Configuration
@EnableAuthorizationServer

protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

@Inject
private DataSource dataSource;

@Inject
private JHipsterProperties jHipsterProperties;

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

/* create OAuth2RequestFactory instance */
private OAuth2RequestFactory oAuth2RequestFactory;

@Inject
@Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints)
    throws Exception {
    /* assign value in OAuth2RequestFactory instance */
    oAuth2RequestFactory = endpoints.getOAuth2RequestFactory();
    endpoints
        .tokenStore(tokenStore())
        .authenticationManager(authenticationManager);
}

@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
    /* register TokenEndpointAuthenticationFilter with oauthServer */
    oauthServer
        .allowFormAuthenticationForClients()
        .addTokenEndpointAuthenticationFilter(new CustomTokenEndpointAuthenticationFilter(authenticationManager, oAuth2RequestFactory));

}

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients
        .inMemory()
        .withClient(jHipsterProperties.getSecurity().getAuthentication().getOauth().getClientid())
        .scopes("read", "write")
        .authorities(AuthoritiesConstants.ADMIN, AuthoritiesConstants.USER)
        .authorizedGrantTypes("password", "refresh_token", "authorization_code", "implicit")
        .secret(jHipsterProperties.getSecurity().getAuthentication().getOauth().getSecret())
        .accessTokenValiditySeconds(jHipsterProperties.getSecurity().getAuthentication().getOauth().getTokenValidityInSeconds());
}
}

通过此设置调用此过滤器,但会发生异常:

  

"未找到客户端身份验证。记得在TokenEndpointAuthenticationFilter"上游放置一个过滤器。

在进一步检查实现过滤器 TokenEndpointAuthenticationFilter 类时,当SecurityContextHolder.getContext()。getAuthentication为null 时抛出此异常

Authentication clientAuth = SecurityContextHolder.getContext().getAuthentication();
                if (clientAuth == null) {
                    throw new BadCredentialsException("No client authentication found. Remember to put a filter upstream of the TokenEndpointAuthenticationFilter.");
                }

我在很多网站上搜索过,但是对这个类的实现的支持几乎不存在。 提前谢谢。

0 个答案:

没有答案