使用 Spring OAuth 将来自 OAuth2 服务器的令牌存储在 cookie 中
Store token from OAuth2 server in cookie using Spring OAuth
Spring OAuth2 是否提供任何配置来创建带有不透明或 JWT 令牌的 cookie?
到目前为止,我在 Internet 上找到的配置描述了授权服务器及其客户端的创建。在我的例子中,客户端是一个网关,上面有一个 Angular 4 应用程序,在同一个部署中。前端向通过 Zuul 路由它们的网关发出请求。
使用 @EnableOAuth2Sso
、application.yml 和 WebSecurityConfigurerAdapter 配置客户端进行所有必要的请求和重定向,将信息添加到 SecurityContext 但将信息存储在会话中,将 JSESSIONID cookie 发送回 UI。
是否需要任何配置或过滤器来创建带有令牌信息的 cookie,然后使用我可以使用的无状态会话?还是我必须自己创建它,然后创建一个过滤器来查找令牌?
@SpringBootApplication
@EnableOAuth2Sso
@RestController
public class ClientApplication extends WebSecurityConfigurerAdapter{
@RequestMapping("/user")
public String home(Principal user) {
return "Hello " + user.getName();
}
public static void main(String[] args) {
new SpringApplicationBuilder(ClientApplication.class).run(args);
}
@Override
public void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/**").authorizeRequests()
.antMatchers("/", "/login**", "/webjars/**").permitAll()
.anyRequest()
.authenticated()
.and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
}
server:
port: 9999
context-path: /client
security:
oauth2:
client:
clientId: acme
clientSecret: acmesecret
accessTokenUri: http://localhost:9080/uaa/oauth/token
userAuthorizationUri: http://localhost:9080/uaa/oauth/authorize
tokenName: access_token
authenticationScheme: query
clientAuthenticationScheme: form
resource:
userInfoUri: http://localhost:9080/uaa/me
确保您已导入 javax.servlet 中存在的这些 类:
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
像这样初始化 cookie:
Cookie jwtCookie = new Cookie(APP_COOKIE_TOKEN, token.getToken());
jwtCookie.setPath("/");
jwtCookie.setMaxAge(20*60);
//Cookie cannot be accessed via JavaScript
jwtCookie.setHttpOnly(true);
在 HttpServletResponse 中添加 cookie:
response.addCookie(jwtCookie);
如果您正在使用 angular 4 和 spring security+boot ,那么 this github repo 可以成为一个很大的帮助:
此存储库的参考 blog 是:
我相信 Spring 对此的默认立场是我们都应该使用 HTTP 会话存储,如果需要,使用 Redis(或等价物)进行复制。对于完全不会飞的完全无状态环境。
正如您所发现的,我的解决方案是添加前置 post 过滤器以在需要时去除和添加 cookie。您还应该查看 OAuth2ClientConfiguration.. 这定义了会话范围的 bean OAuth2ClientContext。为了简单起见,我更改了自动配置并使该 bean 请求具有范围。只需在剥离 cookie 的预过滤器中调用 setAccessToken。
我最终解决了这个问题,方法是创建一个使用令牌创建 cookie 的过滤器,并为 Spring 安全性添加两个配置,一个用于 cookie 在请求中,一个用于 cookie 在请求中时'吨。
我有点认为这对于应该相对简单的事情来说工作太多了,所以我可能遗漏了一些关于整个事情应该如何工作的东西。
public class TokenCookieCreationFilter extends OncePerRequestFilter {
public static final String ACCESS_TOKEN_COOKIE_NAME = "token";
private final UserInfoRestTemplateFactory userInfoRestTemplateFactory;
@Override
protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain filterChain) throws ServletException, IOException {
try {
final OAuth2ClientContext oAuth2ClientContext = userInfoRestTemplateFactory.getUserInfoRestTemplate().getOAuth2ClientContext();
final OAuth2AccessToken authentication = oAuth2ClientContext.getAccessToken();
if (authentication != null && authentication.getExpiresIn() > 0) {
log.debug("Authentication is not expired: expiresIn={}", authentication.getExpiresIn());
final Cookie cookieToken = createCookie(authentication.getValue(), authentication.getExpiresIn());
response.addCookie(cookieToken);
log.debug("Cookied added: name={}", cookieToken.getName());
}
} catch (final Exception e) {
log.error("Error while extracting token for cookie creation", e);
}
filterChain.doFilter(request, response);
}
private Cookie createCookie(final String content, final int expirationTimeSeconds) {
final Cookie cookie = new Cookie(ACCESS_TOKEN_COOKIE_NAME, content);
cookie.setMaxAge(expirationTimeSeconds);
cookie.setHttpOnly(true);
cookie.setPath("/");
return cookie;
}
}
/**
* Adds the authentication information to the SecurityContext. Needed to allow access to restricted paths after a
* successful authentication redirects back to the application. Without it, the filter
* {@link org.springframework.security.web.authentication.AnonymousAuthenticationFilter} cannot find a user
* and rejects access, redirecting to the login page again.
*/
public class SecurityContextRestorerFilter extends OncePerRequestFilter {
private final UserInfoRestTemplateFactory userInfoRestTemplateFactory;
private final ResourceServerTokenServices userInfoTokenServices;
@Override
public void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) throws IOException, ServletException {
try {
final OAuth2AccessToken authentication = userInfoRestTemplateFactory.getUserInfoRestTemplate().getOAuth2ClientContext().getAccessToken();
if (authentication != null && authentication.getExpiresIn() > 0) {
OAuth2Authentication oAuth2Authentication = userInfoTokenServices.loadAuthentication(authentication.getValue());
SecurityContextHolder.getContext().setAuthentication(oAuth2Authentication);
log.debug("Added token authentication to security context");
} else {
log.debug("Authentication not found.");
}
chain.doFilter(request, response);
} finally {
SecurityContextHolder.clearContext();
}
}
}
这是请求中包含 cookie 时的配置。
@RequiredArgsConstructor
@EnableOAuth2Sso
@Configuration
public static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private final UserInfoRestTemplateFactory userInfoRestTemplateFactory;
private final ResourceServerTokenServices userInfoTokenServices;
/**
* Filters are created directly here instead of creating them as Spring beans to avoid them being added as filters * by ResourceServerConfiguration security configuration. This way, they are only executed when the api gateway * behaves as a SSO client.
*/
@Override
protected void configure(final HttpSecurity http) throws Exception {
http
.requestMatcher(withoutCookieToken())
.authorizeRequests()
.antMatchers("/login**", "/oauth/**")
.permitAll()
.anyRequest()
.authenticated()
.and()
.exceptionHandling().authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/login"))
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.csrf().requireCsrfProtectionMatcher(csrfRequestMatcher()).csrfTokenRepository(csrfTokenRepository())
.and()
.addFilterAfter(new TokenCookieCreationFilter(userInfoRestTemplateFactory), AbstractPreAuthenticatedProcessingFilter.class)
.addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class)
.addFilterBefore(new SecurityContextRestorerFilter(userInfoRestTemplateFactory, userInfoTokenServices), AnonymousAuthenticationFilter.class);
}
private RequestMatcher withoutCookieToken() {
return request -> request.getCookies() == null || Arrays.stream(request.getCookies()).noneMatch(cookie -> cookie.getName().equals(ACCESS_TOKEN_COOKIE_NAME));
}
这是当有带有令牌的 cookie 时的配置。有一个 cookie 提取器扩展了 Spring
的 BearerTokenExtractor
功能以在 cookie 中搜索令牌,还有一个身份验证入口点,它在身份验证失败时使 cookie 过期。
@EnableResourceServer
@Configuration
public static class ResourceSecurityServerConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(final ResourceServerSecurityConfigurer resources) {
resources.tokenExtractor(new BearerCookiesTokenExtractor());
resources.authenticationEntryPoint(new InvalidTokenEntryPoint());
}
@Override
public void configure(final HttpSecurity http) throws Exception {
http.requestMatcher(withCookieToken())
.authorizeRequests()
.... security config
.and()
.exceptionHandling().authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/"))
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.logout().logoutSuccessUrl("/your-logging-out-endpoint").permitAll();
}
private RequestMatcher withCookieToken() {
return request -> request.getCookies() != null && Arrays.stream(request.getCookies()).anyMatch(cookie -> cookie.getName().equals(ACCESS_TOKEN_COOKIE_NAME));
}
}
/**
* {@link TokenExtractor} created to check whether there is a token stored in a cookie if there wasn't any in a header
* or a parameter. In that case, it returns a {@link PreAuthenticatedAuthenticationToken} containing its value.
*/
@Slf4j
public class BearerCookiesTokenExtractor implements TokenExtractor {
private final BearerTokenExtractor tokenExtractor = new BearerTokenExtractor();
@Override
public Authentication extract(final HttpServletRequest request) {
Authentication authentication = tokenExtractor.extract(request);
if (authentication == null) {
authentication = Arrays.stream(request.getCookies())
.filter(isValidTokenCookie())
.findFirst()
.map(cookie -> new PreAuthenticatedAuthenticationToken(cookie.getValue(), EMPTY))
.orElseGet(null);
}
return authentication;
}
private Predicate<Cookie> isValidTokenCookie() {
return cookie -> cookie.getName().equals(ACCESS_TOKEN_COOKIE_NAME);
}
}
/**
* Custom entry point used by {@link org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter}
* to remove the current cookie with the access token, redirect the browser to the home page and invalidate the
* OAuth2 session. Related to the session, it is invalidated to destroy the {@link org.springframework.security.oauth2.client.DefaultOAuth2ClientContext}
* that keeps the token in session for when the gateway behaves as an OAuth2 client.
* For further details, {@link org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2RestOperationsConfiguration.SessionScopedConfiguration.ClientContextConfiguration}
*/
@Slf4j
public class InvalidTokenEntryPoint implements AuthenticationEntryPoint {
public static final String CONTEXT_PATH = "/";
@Override
public void commence(final HttpServletRequest request, final HttpServletResponse response, final AuthenticationException authException) throws IOException, ServletException {
log.info("Invalid token used. Destroying cookie and session and redirecting to home page");
request.getSession().invalidate(); //Destroys the DefaultOAuth2ClientContext that keeps the invalid token
response.addCookie(createEmptyCookie());
response.sendRedirect(CONTEXT_PATH);
}
private Cookie createEmptyCookie() {
final Cookie cookie = new Cookie(TokenCookieCreationFilter.ACCESS_TOKEN_COOKIE_NAME, EMPTY);
cookie.setMaxAge(0);
cookie.setHttpOnly(true);
cookie.setPath(CONTEXT_PATH);
return cookie;
}
}
Spring OAuth2 是否提供任何配置来创建带有不透明或 JWT 令牌的 cookie?
到目前为止,我在 Internet 上找到的配置描述了授权服务器及其客户端的创建。在我的例子中,客户端是一个网关,上面有一个 Angular 4 应用程序,在同一个部署中。前端向通过 Zuul 路由它们的网关发出请求。
使用 @EnableOAuth2Sso
、application.yml 和 WebSecurityConfigurerAdapter 配置客户端进行所有必要的请求和重定向,将信息添加到 SecurityContext 但将信息存储在会话中,将 JSESSIONID cookie 发送回 UI。
是否需要任何配置或过滤器来创建带有令牌信息的 cookie,然后使用我可以使用的无状态会话?还是我必须自己创建它,然后创建一个过滤器来查找令牌?
@SpringBootApplication
@EnableOAuth2Sso
@RestController
public class ClientApplication extends WebSecurityConfigurerAdapter{
@RequestMapping("/user")
public String home(Principal user) {
return "Hello " + user.getName();
}
public static void main(String[] args) {
new SpringApplicationBuilder(ClientApplication.class).run(args);
}
@Override
public void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/**").authorizeRequests()
.antMatchers("/", "/login**", "/webjars/**").permitAll()
.anyRequest()
.authenticated()
.and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
}
server:
port: 9999
context-path: /client
security:
oauth2:
client:
clientId: acme
clientSecret: acmesecret
accessTokenUri: http://localhost:9080/uaa/oauth/token
userAuthorizationUri: http://localhost:9080/uaa/oauth/authorize
tokenName: access_token
authenticationScheme: query
clientAuthenticationScheme: form
resource:
userInfoUri: http://localhost:9080/uaa/me
确保您已导入 javax.servlet 中存在的这些 类:
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
像这样初始化 cookie:
Cookie jwtCookie = new Cookie(APP_COOKIE_TOKEN, token.getToken());
jwtCookie.setPath("/");
jwtCookie.setMaxAge(20*60);
//Cookie cannot be accessed via JavaScript
jwtCookie.setHttpOnly(true);
在 HttpServletResponse 中添加 cookie:
response.addCookie(jwtCookie);
如果您正在使用 angular 4 和 spring security+boot ,那么 this github repo 可以成为一个很大的帮助:
此存储库的参考 blog 是:
我相信 Spring 对此的默认立场是我们都应该使用 HTTP 会话存储,如果需要,使用 Redis(或等价物)进行复制。对于完全不会飞的完全无状态环境。
正如您所发现的,我的解决方案是添加前置 post 过滤器以在需要时去除和添加 cookie。您还应该查看 OAuth2ClientConfiguration.. 这定义了会话范围的 bean OAuth2ClientContext。为了简单起见,我更改了自动配置并使该 bean 请求具有范围。只需在剥离 cookie 的预过滤器中调用 setAccessToken。
我最终解决了这个问题,方法是创建一个使用令牌创建 cookie 的过滤器,并为 Spring 安全性添加两个配置,一个用于 cookie 在请求中,一个用于 cookie 在请求中时'吨。 我有点认为这对于应该相对简单的事情来说工作太多了,所以我可能遗漏了一些关于整个事情应该如何工作的东西。
public class TokenCookieCreationFilter extends OncePerRequestFilter {
public static final String ACCESS_TOKEN_COOKIE_NAME = "token";
private final UserInfoRestTemplateFactory userInfoRestTemplateFactory;
@Override
protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain filterChain) throws ServletException, IOException {
try {
final OAuth2ClientContext oAuth2ClientContext = userInfoRestTemplateFactory.getUserInfoRestTemplate().getOAuth2ClientContext();
final OAuth2AccessToken authentication = oAuth2ClientContext.getAccessToken();
if (authentication != null && authentication.getExpiresIn() > 0) {
log.debug("Authentication is not expired: expiresIn={}", authentication.getExpiresIn());
final Cookie cookieToken = createCookie(authentication.getValue(), authentication.getExpiresIn());
response.addCookie(cookieToken);
log.debug("Cookied added: name={}", cookieToken.getName());
}
} catch (final Exception e) {
log.error("Error while extracting token for cookie creation", e);
}
filterChain.doFilter(request, response);
}
private Cookie createCookie(final String content, final int expirationTimeSeconds) {
final Cookie cookie = new Cookie(ACCESS_TOKEN_COOKIE_NAME, content);
cookie.setMaxAge(expirationTimeSeconds);
cookie.setHttpOnly(true);
cookie.setPath("/");
return cookie;
}
}
/**
* Adds the authentication information to the SecurityContext. Needed to allow access to restricted paths after a
* successful authentication redirects back to the application. Without it, the filter
* {@link org.springframework.security.web.authentication.AnonymousAuthenticationFilter} cannot find a user
* and rejects access, redirecting to the login page again.
*/
public class SecurityContextRestorerFilter extends OncePerRequestFilter {
private final UserInfoRestTemplateFactory userInfoRestTemplateFactory;
private final ResourceServerTokenServices userInfoTokenServices;
@Override
public void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) throws IOException, ServletException {
try {
final OAuth2AccessToken authentication = userInfoRestTemplateFactory.getUserInfoRestTemplate().getOAuth2ClientContext().getAccessToken();
if (authentication != null && authentication.getExpiresIn() > 0) {
OAuth2Authentication oAuth2Authentication = userInfoTokenServices.loadAuthentication(authentication.getValue());
SecurityContextHolder.getContext().setAuthentication(oAuth2Authentication);
log.debug("Added token authentication to security context");
} else {
log.debug("Authentication not found.");
}
chain.doFilter(request, response);
} finally {
SecurityContextHolder.clearContext();
}
}
}
这是请求中包含 cookie 时的配置。
@RequiredArgsConstructor
@EnableOAuth2Sso
@Configuration
public static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private final UserInfoRestTemplateFactory userInfoRestTemplateFactory;
private final ResourceServerTokenServices userInfoTokenServices;
/**
* Filters are created directly here instead of creating them as Spring beans to avoid them being added as filters * by ResourceServerConfiguration security configuration. This way, they are only executed when the api gateway * behaves as a SSO client.
*/
@Override
protected void configure(final HttpSecurity http) throws Exception {
http
.requestMatcher(withoutCookieToken())
.authorizeRequests()
.antMatchers("/login**", "/oauth/**")
.permitAll()
.anyRequest()
.authenticated()
.and()
.exceptionHandling().authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/login"))
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.csrf().requireCsrfProtectionMatcher(csrfRequestMatcher()).csrfTokenRepository(csrfTokenRepository())
.and()
.addFilterAfter(new TokenCookieCreationFilter(userInfoRestTemplateFactory), AbstractPreAuthenticatedProcessingFilter.class)
.addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class)
.addFilterBefore(new SecurityContextRestorerFilter(userInfoRestTemplateFactory, userInfoTokenServices), AnonymousAuthenticationFilter.class);
}
private RequestMatcher withoutCookieToken() {
return request -> request.getCookies() == null || Arrays.stream(request.getCookies()).noneMatch(cookie -> cookie.getName().equals(ACCESS_TOKEN_COOKIE_NAME));
}
这是当有带有令牌的 cookie 时的配置。有一个 cookie 提取器扩展了 Spring
的 BearerTokenExtractor
功能以在 cookie 中搜索令牌,还有一个身份验证入口点,它在身份验证失败时使 cookie 过期。
@EnableResourceServer
@Configuration
public static class ResourceSecurityServerConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(final ResourceServerSecurityConfigurer resources) {
resources.tokenExtractor(new BearerCookiesTokenExtractor());
resources.authenticationEntryPoint(new InvalidTokenEntryPoint());
}
@Override
public void configure(final HttpSecurity http) throws Exception {
http.requestMatcher(withCookieToken())
.authorizeRequests()
.... security config
.and()
.exceptionHandling().authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/"))
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.logout().logoutSuccessUrl("/your-logging-out-endpoint").permitAll();
}
private RequestMatcher withCookieToken() {
return request -> request.getCookies() != null && Arrays.stream(request.getCookies()).anyMatch(cookie -> cookie.getName().equals(ACCESS_TOKEN_COOKIE_NAME));
}
}
/**
* {@link TokenExtractor} created to check whether there is a token stored in a cookie if there wasn't any in a header
* or a parameter. In that case, it returns a {@link PreAuthenticatedAuthenticationToken} containing its value.
*/
@Slf4j
public class BearerCookiesTokenExtractor implements TokenExtractor {
private final BearerTokenExtractor tokenExtractor = new BearerTokenExtractor();
@Override
public Authentication extract(final HttpServletRequest request) {
Authentication authentication = tokenExtractor.extract(request);
if (authentication == null) {
authentication = Arrays.stream(request.getCookies())
.filter(isValidTokenCookie())
.findFirst()
.map(cookie -> new PreAuthenticatedAuthenticationToken(cookie.getValue(), EMPTY))
.orElseGet(null);
}
return authentication;
}
private Predicate<Cookie> isValidTokenCookie() {
return cookie -> cookie.getName().equals(ACCESS_TOKEN_COOKIE_NAME);
}
}
/**
* Custom entry point used by {@link org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter}
* to remove the current cookie with the access token, redirect the browser to the home page and invalidate the
* OAuth2 session. Related to the session, it is invalidated to destroy the {@link org.springframework.security.oauth2.client.DefaultOAuth2ClientContext}
* that keeps the token in session for when the gateway behaves as an OAuth2 client.
* For further details, {@link org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2RestOperationsConfiguration.SessionScopedConfiguration.ClientContextConfiguration}
*/
@Slf4j
public class InvalidTokenEntryPoint implements AuthenticationEntryPoint {
public static final String CONTEXT_PATH = "/";
@Override
public void commence(final HttpServletRequest request, final HttpServletResponse response, final AuthenticationException authException) throws IOException, ServletException {
log.info("Invalid token used. Destroying cookie and session and redirecting to home page");
request.getSession().invalidate(); //Destroys the DefaultOAuth2ClientContext that keeps the invalid token
response.addCookie(createEmptyCookie());
response.sendRedirect(CONTEXT_PATH);
}
private Cookie createEmptyCookie() {
final Cookie cookie = new Cookie(TokenCookieCreationFilter.ACCESS_TOKEN_COOKIE_NAME, EMPTY);
cookie.setMaxAge(0);
cookie.setHttpOnly(true);
cookie.setPath(CONTEXT_PATH);
return cookie;
}
}