我正在考虑将OAuth2用于我的应用程序.我想要实现的架构如下:
到目前为止,我已设法在3个基本应用程序(1个auth服务器,1个资源服务器和1个客户端)之间实现此交互.我没有工作的是注销功能.我已经读过Dave Syer在他的教程中描述的"臭名昭着的棘手问题",但在这种情况下我真的需要用户在注销后重新登录.我已经尝试给访问令牌和刷新令牌提供几秒钟,但是当到期时,我没有被提示再次登录,而是在客户端应用程序上获得NPE.我也尝试过这篇文章中提出的解决方案来从令牌存储中删除令牌,但它不起作用.单点注销对我来说是这种实现的理想行为.如何使用Spring Boot Oauth2实现此目的.如果由于某种原因不可能,我可以使用哪些替代方法来实现使用Spring Boot的集中安全性?
提前致谢.
java spring spring-security spring-boot spring-security-oauth2
如何触发此示例Spring Boot OAuth2应用程序的自动注销?
我尝试将以下代码从另一个帖子的答案中添加到应用程序demo包中的新控制器类中: authserver
package demo;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.http.HttpStatus;
@Controller
public class OAuthController {
@Autowired
private TokenStore tokenStore;
@RequestMapping(value = "/oauth/revoke-token", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public void logout(HttpServletRequest request) {
String authHeader = request.getHeader("Authorization");
if (authHeader != null) {
String tokenValue = authHeader.replace("Bearer", "").trim();
OAuth2AccessToken accessToken = tokenStore.readAccessToken(tokenValue);
tokenStore.removeAccessToken(accessToken);
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试启动应用程序时,调试日志会显示以下错误,指示它不能autowire存储令牌存储:
Caused by: org.springframework.beans.factory.BeanCreationException: …Run Code Online (Sandbox Code Playgroud)