在 Spring 安全 OAuth2 实施中为成功的 OAuth2 身份验证设置 cookie
Set cookies on successful OAuth2 Authentication in Spring Security OAuth2 implementation
我正在根据 https://spring.io/guides/tutorials/spring-boot-oauth2/
中提供的指南实施一个稍微简单的 OAuth2 安全 Web 应用程序
我需要在成功登录后设置一些任意 cookie 以简化我的前端浏览器应用程序中的操作。
目前我有一个可用的设置,可以使用 OAuth2 对具有 Google 帐户的用户进行身份验证。
我打算在我的 WebSecurityConfigurerAdapter
configure()
函数中使用 HttpSecurity
oauth2Login().successHandler()
但是我没有提供 ClientRegistrationRepository
而且我似乎也没有能够自动装配它。
我似乎无法在任何地方找到关于如何向该指南中介绍的实现添加额外的登录成功逻辑的任何标准方法。
这是我的主要应用程序 class,OAuth2 客户端在 application.yml 文件中配置。
@SpringBootApplication
@EnableOAuth2Client
public class RestApplication extends WebSecurityConfigurerAdapter {
@Autowired
LogoutSuccessHandler logoutHandler;
@Autowired
OAuth2ClientContext oauth2ClientContext;
public static void main(String[] args) {
SpringApplication.run(RestApplication.class, args);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.antMatcher("/**").authorizeRequests()
.antMatchers("/", "/login**", "/error**", "/webapp/**").permitAll()
.anyRequest().authenticated()
.and().addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class)
.logout().logoutSuccessUrl("/").invalidateHttpSession(true).clearAuthentication(true).deleteCookies("JSESSIONID").logoutSuccessHandler(logoutHandler)
// @formatter:on
}
private Filter ssoFilter() {
OAuth2ClientAuthenticationProcessingFilter authFilter = new OAuth2ClientAuthenticationProcessingFilter(
"/login");
OAuth2RestTemplate oAuthTemplate = new OAuth2RestTemplate(oAuth2ResourceDetails(), oauth2ClientContext);
UserInfoTokenServices tokenServices = new UserInfoTokenServices(oAuth2Resource().getUserInfoUri(),
oAuth2ResourceDetails().getClientId());
authFilter.setRestTemplate(oAuthTemplate);
tokenServices.setRestTemplate(oAuthTemplate);
authFilter.setTokenServices(tokenServices);
return authFilter;
}
@Bean
@ConfigurationProperties("oauth.client")
public AuthorizationCodeResourceDetails oAuth2ResourceDetails() {
return new AuthorizationCodeResourceDetails();
}
@Bean
@ConfigurationProperties("oauth.resource")
public ResourceServerProperties oAuth2Resource() {
return new ResourceServerProperties();
}
@Bean
public FilterRegistrationBean<OAuth2ClientContextFilter> oauth2ClientFilterRegistration(
OAuth2ClientContextFilter filter) {
FilterRegistrationBean<OAuth2ClientContextFilter> registration = new FilterRegistrationBean<OAuth2ClientContextFilter>();
registration.setFilter(filter);
registration.setOrder(-100);
return registration;
}
}
添加在成功身份验证期间发生一次的逻辑的正确方法是什么,特别是在我有权访问用户 Principal 对象之后。
我进一步研究了 OAuth2ClientAuthenticationProcessingFilter
实现并找到了以下可能的解决方案。
可以插入默认情况下未实现的自定义 SessionAuthenticationStrategy
。接口文档说明如下:
Allows pluggable support for HttpSession-related behaviour when an authentication occurs.
我已将 ssoFilter()
更改为以下内容:
private Filter ssoFilter() {
OAuth2ClientAuthenticationProcessingFilter authFilter = new OAuth2ClientAuthenticationProcessingFilter(
"/login");
authFilter.setSessionAuthenticationStrategy(new SessionAuthenticationStrategy() {
@Override
public void onAuthentication(Authentication authentication, HttpServletRequest request,
HttpServletResponse response) throws SessionAuthenticationException {
LinkedHashMap<String, Object> userDets = (LinkedHashMap<String, Object>) ((OAuth2Authentication) authentication)
.getUserAuthentication().getDetails();
response.addCookie(new Cookie("authenticated", userDets.get("email").toString()));
}
});
OAuth2RestTemplate oAuthTemplate = new OAuth2RestTemplate(oAuth2ResourceDetails(), oauth2ClientContext);
UserInfoTokenServices tokenServices = new UserInfoTokenServices(oAuth2Resource().getUserInfoUri(),
oAuth2ResourceDetails().getClientId());
authFilter.setRestTemplate(oAuthTemplate);
tokenServices.setRestTemplate(oAuthTemplate);
authFilter.setTokenServices(tokenServices);
return authFilter;
}
我正在根据 https://spring.io/guides/tutorials/spring-boot-oauth2/
中提供的指南实施一个稍微简单的 OAuth2 安全 Web 应用程序我需要在成功登录后设置一些任意 cookie 以简化我的前端浏览器应用程序中的操作。
目前我有一个可用的设置,可以使用 OAuth2 对具有 Google 帐户的用户进行身份验证。
我打算在我的 WebSecurityConfigurerAdapter
configure()
函数中使用 HttpSecurity
oauth2Login().successHandler()
但是我没有提供 ClientRegistrationRepository
而且我似乎也没有能够自动装配它。
我似乎无法在任何地方找到关于如何向该指南中介绍的实现添加额外的登录成功逻辑的任何标准方法。
这是我的主要应用程序 class,OAuth2 客户端在 application.yml 文件中配置。
@SpringBootApplication
@EnableOAuth2Client
public class RestApplication extends WebSecurityConfigurerAdapter {
@Autowired
LogoutSuccessHandler logoutHandler;
@Autowired
OAuth2ClientContext oauth2ClientContext;
public static void main(String[] args) {
SpringApplication.run(RestApplication.class, args);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.antMatcher("/**").authorizeRequests()
.antMatchers("/", "/login**", "/error**", "/webapp/**").permitAll()
.anyRequest().authenticated()
.and().addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class)
.logout().logoutSuccessUrl("/").invalidateHttpSession(true).clearAuthentication(true).deleteCookies("JSESSIONID").logoutSuccessHandler(logoutHandler)
// @formatter:on
}
private Filter ssoFilter() {
OAuth2ClientAuthenticationProcessingFilter authFilter = new OAuth2ClientAuthenticationProcessingFilter(
"/login");
OAuth2RestTemplate oAuthTemplate = new OAuth2RestTemplate(oAuth2ResourceDetails(), oauth2ClientContext);
UserInfoTokenServices tokenServices = new UserInfoTokenServices(oAuth2Resource().getUserInfoUri(),
oAuth2ResourceDetails().getClientId());
authFilter.setRestTemplate(oAuthTemplate);
tokenServices.setRestTemplate(oAuthTemplate);
authFilter.setTokenServices(tokenServices);
return authFilter;
}
@Bean
@ConfigurationProperties("oauth.client")
public AuthorizationCodeResourceDetails oAuth2ResourceDetails() {
return new AuthorizationCodeResourceDetails();
}
@Bean
@ConfigurationProperties("oauth.resource")
public ResourceServerProperties oAuth2Resource() {
return new ResourceServerProperties();
}
@Bean
public FilterRegistrationBean<OAuth2ClientContextFilter> oauth2ClientFilterRegistration(
OAuth2ClientContextFilter filter) {
FilterRegistrationBean<OAuth2ClientContextFilter> registration = new FilterRegistrationBean<OAuth2ClientContextFilter>();
registration.setFilter(filter);
registration.setOrder(-100);
return registration;
}
}
添加在成功身份验证期间发生一次的逻辑的正确方法是什么,特别是在我有权访问用户 Principal 对象之后。
我进一步研究了 OAuth2ClientAuthenticationProcessingFilter
实现并找到了以下可能的解决方案。
可以插入默认情况下未实现的自定义 SessionAuthenticationStrategy
。接口文档说明如下:
Allows pluggable support for HttpSession-related behaviour when an authentication occurs.
我已将 ssoFilter()
更改为以下内容:
private Filter ssoFilter() {
OAuth2ClientAuthenticationProcessingFilter authFilter = new OAuth2ClientAuthenticationProcessingFilter(
"/login");
authFilter.setSessionAuthenticationStrategy(new SessionAuthenticationStrategy() {
@Override
public void onAuthentication(Authentication authentication, HttpServletRequest request,
HttpServletResponse response) throws SessionAuthenticationException {
LinkedHashMap<String, Object> userDets = (LinkedHashMap<String, Object>) ((OAuth2Authentication) authentication)
.getUserAuthentication().getDetails();
response.addCookie(new Cookie("authenticated", userDets.get("email").toString()));
}
});
OAuth2RestTemplate oAuthTemplate = new OAuth2RestTemplate(oAuth2ResourceDetails(), oauth2ClientContext);
UserInfoTokenServices tokenServices = new UserInfoTokenServices(oAuth2Resource().getUserInfoUri(),
oAuth2ResourceDetails().getClientId());
authFilter.setRestTemplate(oAuthTemplate);
tokenServices.setRestTemplate(oAuthTemplate);
authFilter.setTokenServices(tokenServices);
return authFilter;
}