gpt4 book ai didi

java - spring-oauth2 登录成功处理器

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:59:56 24 4
gpt4 key购买 nike

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

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

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

TIA

最佳答案

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

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();
}
}

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

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 之后

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) {
}

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

有关更多信息,您可以在 github 上引用此代码

已编辑:

当你有默认的 token 响应时,上面的代码片段工作正常,它只是使用 ServletResponseWrapper 和提取。但它似乎仍然存在漏洞,因此您可以通过 org.springframework.security.oauth2.provider.token.TokenEnhancer

了解用户身份验证是否成功

关注这个answer了解详情。

关于java - spring-oauth2 登录成功处理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29339027/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com