首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >spring-oauth2登录成功处理程序

spring-oauth2登录成功处理程序
EN

Stack Overflow用户
提问于 2015-03-30 13:33:36
回答 2查看 4.1K关注 0票数 11

有没有办法使用spring-oauth2添加登录成功处理程序?

我尝试使用基本身份验证筛选器,但它只过滤客户端凭据,而不是用户凭据。

或者,我是否需要创建自定义用户身份验证管理器?

提亚

EN

回答 2

Stack Overflow用户

发布于 2016-01-09 16:56:13

这个解决方案是否适用于密码流和其他我不确定。您可以在http标签中的"before=BASIC_AUTH_FILTER“位置添加您的自定义过滤器,该标签位于oauth-server配置中,您可以通过解析"oauth/token”的响应来实现,因此创建ByteArrayResponseWrapper来获取响应,这里我使用来自"org.apache.commons commons-io“的TeeOutputStream类,

代码语言:javascript
复制
private class ByteArrayResponseWrapper extends HttpServletResponseWrapper {

    public ByteArrayResponseWrapper(ServletResponse response) {
        super((HttpServletResponse) response);
    }

    private ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        return new DelegatingServletOutputStream(new TeeOutputStream(
                super.getOutputStream(), byteArrayOutputStream));
    }

    public byte[] getByteArray() {
        return this.byteArrayOutputStream.toByteArray();
    }
}

并且我创建了令牌提取器来分离提取access_token的代码

代码语言:javascript
复制
public class OAuth2AccessTokenExtractor implements
    OAuth2AccessTokenExtractor {

private ObjectMapper mapper = new ObjectMapper();

public String getAccessTokenValue(byte[] response) {
    try {
        return mapper.readValue(response, OAuth2AccessToken.class)
                .getValue();
    } catch (JsonParseException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
 }

}

创建过滤器覆盖doFilter后,如下所示

代码语言:javascript
复制
private DefaultTokenServices tokenServices;

private OAuth2AccessTokenExtractor tokenExtractor;

@Override
public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain) throws IOException, ServletException {

    // create wrapper to read response body
    ByteArrayResponseWrapper responseWraper = new ByteArrayResponseWrapper(
            response);

    // led them go
    chain.doFilter(request, responseWraper);

    // get ClientAuthentication
    Authentication clientAuthentication = SecurityContextHolder
            .getContext().getAuthentication();

    // is authenticated or not to proceed
    if (clientAuthentication != null
            && clientAuthentication.isAuthenticated()) {

        // callBack client authenticated successfully
        onSuccessfulClientAuthentication(request, response,
                clientAuthentication);

        // check response status is success of failure
        if (responseWraper.getStatus() == 200) {

            // extract accessToken from response
            String token = tokenExtractor
                    .getAccessTokenValue(responseWraper.getByteArray());

            if (token != null && !token.isEmpty()) {

                // load authentication from token
                OAuth2Authentication oAuth2Authentication = this.tokenServices
                        .loadAuthentication(token);
                OAuth2AccessToken actualAccessToken = this.tokenServices
                        .getAccessToken(oAuth2Authentication);

                // callBack user authenticated successfully
                onSuccessfulUserAuthentication(request, response,
                        clientAuthentication, oAuth2Authentication,
                        actualAccessToken);
            } else {
                log.error("access token is empty from extractor");
            }
        } else {
            // callBack user authenticated failure
            onFailureUserAuthentication(request, response,
                    clientAuthentication, request.getParameter("username"));
        }
    } else {
        // callBack client authenticated failure
        onFailClientAuthentication(request, response,
                request.getParameter(OAuth2Utils.CLIENT_ID));
    }
}

protected void onSuccessfulClientAuthentication(ServletRequest request,
        ServletResponse response, Authentication authentication) {
}

protected void onFailClientAuthentication(ServletRequest request,
        ServletResponse response, String clientId) {
}

protected void onSuccessfulUserAuthentication(ServletRequest request,
        ServletResponse response, Authentication clientAuthentication,
        OAuth2Authentication userOAuth2Authentication,
        OAuth2AccessToken token) {
}

protected void onFailureUserAuthentication(ServletRequest request,
        ServletResponse response, Authentication clientAuthentication,
        String username) {
}

在创建过滤器实例时注入tokenServices。现在将根据您的身份验证调用onSuccessfulClientAuthentication、onFailClientAuthentication、onSuccessfulUserAuthentication和onFailureUserAuthentication

有关更多信息,请参阅github上的代码

编辑:

当你有默认的令牌响应时,上面的代码片段工作得很好,它只是使用了ServletResponseWrapper和extracting。但是它仍然很容易受到攻击,所以您可以通过org.springframework.security.oauth2.provider.token.TokenEnhancer类了解用户身份验证的成功情况

有关详细信息,请遵循此answer

票数 5
EN

Stack Overflow用户

发布于 2015-08-19 01:22:33

我们构建了一个自定义身份验证管理器,并将其连接到OAuth2AuthenticationProcessingFilter中以完成此任务。管理器的身份验证方法能够从身份验证主体解压OAuth2Authentication和OAuth2AuthenticationDetails。

代码语言:javascript
复制
<bean id="oAuth2AuthenticationManager" class="org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationManager">
    <property name="resourceId" value="XXX-api"/>
    <property name="tokenServices" ref="tokenServices"/>
</bean>

<bean id="resourceServerFilter"
      class="org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter">
    <property name="authenticationManager" ref="oAuth2AuthenticationManager"/>
    <property name="tokenExtractor">
        <bean class="com.xxx.oauth.BearerTokenExtractor"/>
    </property>
</bean>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29339027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档