gpt4 book ai didi

spring-security - 用于 Google 登录的 Spring Security OAuth2 Java 配置

转载 作者:行者123 更新时间:2023-12-04 12:53:54 30 4
gpt4 key购买 nike

我正在将工作 XML 配置迁移到 Spring Security OAuth2 的 Java 配置,并使用 Google 作为 OAuth 提供者。

这就是我的 java 配置的样子:

@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

private static final List<String> scope;

static {
// Permissions to access email and profile
scope = new ArrayList<>(3);
scope.add("openid");
scope.add("email");
scope.add("profile");
}

@Autowired(required = true)
private UserService userService;

@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}

@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http.
authorizeRequests()
.antMatchers(HttpMethod.GET, "/","/public/**", "/resources/**","/resources/public/**").permitAll()
//.antMatchers("/google_oauth2_login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/")
.loginProcessingUrl("/login")
.defaultSuccessUrl("/home")
.and()
.csrf().disable()
.logout()
.logoutSuccessUrl("/")
.logoutUrl("/logout")
.and()
.requiresChannel().anyRequest().requiresSecure()
.and()
.addFilterAfter(oAuth2ClientContextFilter(),ExceptionTranslationFilter.class)
.addFilterAfter(googleOAuth2Filter(),OAuth2ClientContextFilter.class)
.userDetailsService(userService);
// @formatter:on
}

@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
// @formatter:off
auth
.authenticationProvider(googleOauth2AuthProvider())
.userDetailsService(userService);
// @formatter:on
}

@Bean
public GoogleOAuth2Filter googleOAuth2Filter() throws Exception {
GoogleOAuth2Filter filter = new GoogleOAuth2Filter(
"/google_oauth2_login",
"https://accounts.google.com/o/oauth2/auth",
oAuth2RestTemplate(auth2ProtectedResourceDetails()));
filter.setAuthenticationManager(authenticationManagerBean());
return filter;
}

@Bean
public GoogleOauth2AuthProvider googleOauth2AuthProvider() {
GoogleOauth2AuthProvider authProvider = new GoogleOauth2AuthProvider();
return authProvider;
}

@Bean
public OAuth2ProtectedResourceDetails auth2ProtectedResourceDetails() {
AuthorizationCodeResourceDetails auth2ProtectedResourceDetails = new AuthorizationCodeResourceDetails();
auth2ProtectedResourceDetails
.setClientAuthenticationScheme(AuthenticationScheme.form);
auth2ProtectedResourceDetails
.setAuthenticationScheme(AuthenticationScheme.form);
auth2ProtectedResourceDetails.setGrantType("authorization_code");
auth2ProtectedResourceDetails
.setClientId("the-client-id");
auth2ProtectedResourceDetails
.setClientSecret("the-client-secret");
auth2ProtectedResourceDetails
.setAccessTokenUri("https://accounts.google.com/o/oauth2/token");
auth2ProtectedResourceDetails.setScope(scope);
auth2ProtectedResourceDetails
.setUserAuthorizationUri("https://accounts.google.com/o/oauth2/auth");
auth2ProtectedResourceDetails.setUseCurrentUri(false);
auth2ProtectedResourceDetails
.setPreEstablishedRedirectUri("https://localhost/google_oauth2_login");
return auth2ProtectedResourceDetails;
}

@Bean
public OAuth2RestTemplate oAuth2RestTemplate(
OAuth2ProtectedResourceDetails resource) {
OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(resource);
return oAuth2RestTemplate;
}

@Bean
public OAuth2ClientContextFilter oAuth2ClientContextFilter() {
OAuth2ClientContextFilter oAuth2ClientContextFilter = new OAuth2ClientContextFilter();
return oAuth2ClientContextFilter;
}

}

请注意,我已禁用 CSRF。
从我的登录页面,用户会被重定向到 Google 登录页面

问题:-
  • Google 权限页面只要求“具有离线访问权限”。缺少“电子邮件/个人资料”访问请求。

  • 等效的“范围”属性 XML 配置:-

    <oauth2:resource id="googleOauth2Resource" type="authorization_code"
    client-id="the-client-id
    client-secret="the-client-secret"
    user-authorization-uri="https://accounts.google.com/o/oauth2/auth"
    scope="openid email profile" use-current-uri="false"
    client-authentication-scheme="form" pre-established-redirect-uri="https://localhost/google_oauth2_login" />

    正确地要求电子邮件和个人资料权限。为什么?
  • 继续使用“具有脱机访问权限”会导致此异常:-

  •     org.springframework.web.client.HttpClientErrorException: 400 Bad Request
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
    at org.springframework.security.oauth2.client.token.OAuth2AccessTokenSupport$AccessTokenErrorHandler.handleError(OAuth2AccessTokenSupport.java:243)
    at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:592)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:550)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:514)
    at org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider.obtainAuthorizationCode(AuthorizationCodeAccessTokenProvider.java:145)
    at org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider.obtainAccessToken(AuthorizationCodeAccessTokenProvider.java:196)
    at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainNewAccessTokenInternal(AccessTokenProviderChain.java:142)
    at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainAccessToken(AccessTokenProviderChain.java:118)
    at org.springframework.security.oauth2.client.OAuth2RestTemplate.acquireAccessToken(OAuth2RestTemplate.java:221)
    at org.springframework.security.oauth2.client.OAuth2RestTemplate.getAccessToken(OAuth2RestTemplate.java:173)
    at org.springframework.security.oauth2.client.OAuth2RestTemplate.createRequest(OAuth2RestTemplate.java:105)

    在尝试使用此代码块获取用户配置文件时:

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request,
    HttpServletResponse response) throws AuthenticationException,
    IOException, ServletException {
    logger.info("Google Oauth Filter Triggered!!");
    SecurityContext context = SecurityContextHolder.getContext();
    // auth null or not authenticated.
    String code = request.getParameter("code");
    Authentication dummyAuthentication = null;

    if (StringUtils.isEmpty(code)) {
    // Google authentication in progress. will return null.
    logger.debug("Will set dummy user in context ");
    dummyAuthentication = getDummyAuthenticationToken();
    context.setAuthentication(dummyAuthentication);
    // trigger google oauth2.
    oauth2RestTemplate.postForEntity(authURI, null, Object.class);
    return null;
    } else {
    // response from google received !!.
    // remove dummy authentication from context.
    //SecurityContextHolder.clearContext();
    logger.debug("Response from Google Recieved !!");
    // get user profile and prepare the authentication token object.
    ResponseEntity<Object> forEntity = oauth2RestTemplate.getForEntity(
    HTTPS_WWW_GOOGLEAPIS_COM_PLUS_V1_PEOPLE_ME_OPEN_ID_CONNECT,
    Object.class);

    @SuppressWarnings("unchecked")
    Map<String, String> profile = (Map<String, String>) forEntity
    .getBody();

    CustomOAuth2AuthenticationToken authenticationToken = getOAuth2Token(
    profile.get(EMAIL), profile.get(NAME));
    authenticationToken.setAuthenticated(false);
    return getAuthenticationManager().authenticate(authenticationToken);
    }
    }

    Spring RestTemplate 在日志中显示了这一点:
    o.s.web.client.RestTemplate              : POST request for "https://accounts.google.com/o/oauth2/auth" resulted in 400 (Bad Request); invoking error handler
    2014-09-05 21:51:46.870 WARN 5836 --- [ qtp25546756-15] o.eclipse.jetty.servlet.ServletHandler : /google_oauth2_login

    与 XML 配置一起使用时,同样的代码也可以工作。

    更新 1

    我能够通过将范围更改为“ https://www.googleapis.com/auth/plus.profile.emails.read”来解决“离线访问”问题' & ' https://www.googleapis.com/auth/plus.login '。

    尝试获取用户个人资料时仍然出现错误的请求错误

    请在此处找到问题的源代码 -
    git 克隆 https://kumarsambhavjain@bitbucket.org/kumarsambhavjain/spring-oauth2-login.git

    最佳答案

    您是否尝试过将个人资料网址更改为

    https://www.googleapis.com/plus/v1/people/me/openIdConnect

    查看更多: https://developers.google.com/+/api/openidconnect/getOpenIdConnect

    关于spring-security - 用于 Google 登录的 Spring Security OAuth2 Java 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25123766/

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