标签: spring-oauth2

由于cookie冲突(授权代码机制),官方Spring安全性oauth2示例不起作用

根据教程Spring Boot和OAuth2

我有以下项目结构:

在此输入图像描述

以下源代码:

SocialApplication.class:

@SpringBootApplication
@RestController
@EnableOAuth2Client
@EnableAuthorizationServer
@Order(200)
public class SocialApplication extends WebSecurityConfigurerAdapter {

    @Autowired
    OAuth2ClientContext oauth2ClientContext;

    @RequestMapping({ "/user", "/me" })
    public Map<String, String> user(Principal principal) {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("name", principal.getName());
        return map;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        http.antMatcher("/**").authorizeRequests().antMatchers("/", "/login**", "/webjars/**").permitAll().anyRequest()
                .authenticated().and().exceptionHandling()
                .authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/")).and().logout()
                .logoutSuccessUrl("/").permitAll().and().csrf()
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()).and()
                .addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class);
        // @formatter:on
    }

    @Configuration
    @EnableResourceServer
    protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
        @Override
        public void configure(HttpSecurity http) throws Exception …
Run Code Online (Sandbox Code Playgroud)

java spring-security oauth-2.0 spring-oauth2

29
推荐指数
3
解决办法
3763
查看次数

WebSecurityConfigurerAdapter与ResourceServerConfigurerAdapter之间的关系

我正在尝试将Spring OAuth2集成到Spring MVC REST中.大多数Spring OAuth2示例中只有ResourceServerConfigurerAdapter一些也有一些WebSecurityConfigurerAdapter.我不打算将OAuth与Google,Facebook等集成.我正在尝试为Spring MVC REST提供基于令牌的身份验证,该Basic身份验证目前基于身份验证.有人可以在单个服务器中了解Spring MVC REST + OAuth集成需要什么,而不是或者很好的资源吗?

目前我的POC没有WebSecurityConfigurerAdapter,但与之ResourceServerConfigurerAdapter一起工作AuthorizationServerConfigurerAdapter.看起来ResourceServerConfigurerAdapter就够了.现在我不知道我应该怎样对我现有的WebSecurityConfigurerAdapter,在我的Spring MVC REST应用程序中完美运行.

spring-mvc spring-security spring-4 spring-oauth2

27
推荐指数
1
解决办法
2万
查看次数

替换AuthorizationRequest后,HttpSession为null

下面给出了快速重现问题的完整代码和说明.


问题:
HttpSession成为null以后的自定义实现DefaultOAuth2RequestFactory替换当前AuthorizationRequest与保存AuthorizationRequest. 这将导致后续请求失败/oauth/token,因为CsrfFilter在之前的春季安全过滤器链/oauth/token端点无法找到一个session Csrf tokennull session与比较requestCsrf token.


在错误期间控制流量:

以下流程图说明了步骤14步骤15以某种方式 - null通知HttpSession.(或者可能不匹配a JSESSIONID.)在步骤14SYSO开始时的A 表明确实存在确实包含正确的.然而,不知何故,当步骤15触发从URL 处的客户端返回到端点的呼叫时,已经变为. CustomOAuth2RequestFactory.javaHttpSessionCsrfTokenHttpSessionnulllocalhost:8080/loginlocalhost:9999/oauth/token

断点被添加到HttpSessionSecurityContextRepository下面的调试日志中提到的每一行.(它位于在Maven Dependencies所述的夹authservereclipse项目.)这些断点确认HttpSessionnull,当向最终请求/oauth/token在下面的流程图制备.(左下角流程图)null …

spring spring-mvc spring-security spring-boot spring-oauth2

23
推荐指数
1
解决办法
1849
查看次数

Java 17 无法调用“java.lang.reflect.Method.invoke(Object, Object[])”,因为 com.sun.xml.bind.v2.runtime.reflect.opt.Injector.defineClass 为 null

我在 spring boot 中有一个带有 oauth2 的小项目,我想运行资源服务器

设置:

  • 爪哇17
  • spring-boot-starter-parent 版本 2.5.6
  • spring-cloud-dependency 版本 2020.0.4
  • spring-boot-starter-oauth2-资源服务器
  • spring-security-oauth2-自动配置

当我想运行我的应用程序时,我有例外:


org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.NullPointerException: Cannot invoke "java.lang.reflect.Method.invoke(Object, Object[])" because "com.sun.xml.bind.v2.runtime.reflect.opt.Injector.defineClass" is null
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:486) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.12.jar:5.3.12]
    at …
Run Code Online (Sandbox Code Playgroud)

java spring spring-oauth2 java-17

23
推荐指数
3
解决办法
3万
查看次数

应用领域必须与Facebook网页游戏网址(https),移动网站网址,Unity二进制网址,网站网址或安全网页标签网址相匹配

我正在开发Spring Boot + Facebook集成示例.在这个例子中,我创建了一个应用程序https://developers.facebook.com/.下面显示了创建的应用程序的屏幕截图.

当我传递域名时,我收到以下错误:

App domains must match the domain of the Facebook Web Games URL (https), Mobile 
Site URL, Unity Binary URL, Site URL or Secure Page Tab URL. Please correct these
domains: localhost
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我使用了被阻止URL中提到的相同代码:此重定向失败,因为重定向URI未在应用程序的客户端OAuth设置中列入白名单.确保客户端和Web.

如果需要任何其他细节,请指导并告诉我.

java spring-social-facebook spring-boot spring-oauth2

22
推荐指数
3
解决办法
9231
查看次数

如何从OAuth2授权服务器/用户端点获取自定义用户信息

我有一个配置了@EnableResourceServer注释的资源服务器,它通过user-info-uri参数引用授权服务器,如下所示:

security:
  oauth2:
    resource:
      user-info-uri: http://localhost:9001/user
Run Code Online (Sandbox Code Playgroud)


授权服务器/用户端点返回的扩展名org.springframework.security.core.userdetails.User包含例如电子邮件:

{  
   "password":null,
   "username":"myuser",
    ...
   "email":"me@company.com"
}
Run Code Online (Sandbox Code Playgroud)


每当访问某个资源服务器端点时,Spring通过调用授权服务器的/user端点来验证后台的访问令牌,它实际上会返回丰富的用户信息(其中包含例如电子邮件信息,我已经使用Wireshark验证了该信息).

所以问题是如何在没有明确第二次调用授权服务器/user端点的情况下获取此自定义用户信息.Spring是否在授权后将其存储在资源服务器本地的某个位置,或者如果没有任何可用的开箱即用,那么实现此类用户信息的最佳方式是什么?

spring spring-security spring-boot spring-cloud spring-oauth2

19
推荐指数
2
解决办法
5万
查看次数

如何使用AuthorizationServerSecurityConfigurer?

我正在看一个具有以下代码的Spring启动项目:

public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
    oauthServer
        .tokenKeyAccess("permitAll()")
        .checkTokenAccess("isAuthenticated()");
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,我无法在任何地方找到任何资源(例如Google,Spring docs,Spring oauth docs),它向我解释了如何实际使用AuthorizationServerSecurityConfigurer.而且,我不明白究竟做什么tokenKeyAccess("permitAll()")checkTokenAccess("isAuthenticated()")做什么.

除了帮助我理解这两个功能的作用之外,请帮助我了解将来在哪里查找这些类型的信息.

spring spring-mvc spring-security spring-boot spring-oauth2

16
推荐指数
1
解决办法
7923
查看次数

/ oauth/token中的XSRF令牌无效

已将多重身份验证的Spring OAuth2实现的完整代码上载到此链接的文件共享站点.下面给出了说明,只需几分钟即可在任何计算机上重新创建当前问题.


当前问题:


大多数身份验证算法都能正常工作.程序不会中断,直到下面显示的控制流程结束.具体来说,Invalid CSRF token found for http://localhost:9999/uaa/oauth/token在下面的第二次通过结束时会抛出错误.在上面的链接的应用程序中加入一个定制开发OAuth2RequestFactory,TwoFactorAuthenticationFilterTwoFactorAuthenticationControllerauthserver应用此的春天启动了OAuth2 GitHub的样品. 需要对下面的代码进行哪些具体更改才能解决此CSRF令牌错误并启用双因素身份验证?

我的研究使我怀疑CustomOAuth2RequestFactory(此链接上的API)可能是配置解决方案的地方,因为它定义了管理AuthorizationRequests和TokenRequests的方法.

官方OAuth2规范的此部分指示state对授权端点发出的请求的参数csrf是添加令牌的位置.

此外,链接中的代码使用此链接中描述的授权代码授权类型到官方规范,这意味着流程中的步骤C不会更新csrf代码,从而在步骤D中触发错误.(您可以查看整个流程包括官方规范步骤C和步骤D. )


控制流量围绕当前错误:


正在所述过程中引发电流误差第二遍通过TwoFactorAuthenticationFilter在下面的流程图.一切都按预期工作,直到控制流进入第二通道.

以下流程图说明了可下载应用程序中的代码所采用的双因素身份验证过程的控制流程.

具体来说,s和s HTTP序列的Firefox 标题显示,序列中POST的每个请求都会发送GET相同的XSRFcookie.该XSRF标记值不会导致一个问题,直到后POST /secure/two_factor_authentication,其在触发服务器处理/oauth/authorize/oauth/token …

spring spring-mvc spring-security spring-boot spring-oauth2

14
推荐指数
1
解决办法
2551
查看次数

如何在Spring中注销oauth2客户端?

我有最简单的oauth2客户端:

@EnableAutoConfiguration
@Configuration
@EnableOAuth2Sso
@RestController
public class ClientApplication {

    @RequestMapping("/")
    public String home(Principal user, HttpServletRequest request, HttpServletResponse response) throws ServletException {       
        return "Hello " + user.getName();
    }

    public static void main(String[] args) {
        new SpringApplicationBuilder(ClientApplication.class)
                .properties("spring.config.name=application").run(args);
    }

}
Run Code Online (Sandbox Code Playgroud)

我还有以下内容application.yml:

server:
  port: 9999
  servlet:
    context-path: /client
security:
  oauth2:
    client:
      client-id: acme
      client-secret: acmesecret
      access-token-uri: http://localhost:8080/oauth/token
      user-authorization-uri: http://localhost:8080/oauth/authorize
    resource:
      user-info-uri: http://localhost:8080/me

logging:
  level:
    org.springframework.security: DEBUG
    org.springframework.web: DEBUG
Run Code Online (Sandbox Code Playgroud)

这是完整的代码.我没有任何其他源代码.它工作正常.

但现在我想添加一个注销功能.我添加了一个端点,但它不起作用.我试着做以下事情:

@RequestMapping("/logout")
    public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        Authentication …
Run Code Online (Sandbox Code Playgroud)

java spring-security oauth-2.0 spring-security-oauth2 spring-oauth2

14
推荐指数
3
解决办法
4209
查看次数

如何使用Spring Cloud Security实现OAuth2"令牌交换"

我想知道是否有人举例说明如何使用Spring Cloud Security(使用OAuth2)实现"令牌交换"技术.

目前,我已经在微服务环境中使用ZuulProxy实现了"令牌中继"技术,以"中继"OAuth2令牌并实现SSO.这很好,但暗示每个微服务使用相同的clientId(在ZuulProxy设置中指定为ZuulProxy仅使用authorization_code grant类型和clientId提供的中继).但是,对于微服务内部调用,我想"交换"令牌.这意味着在某些情况下,ZuulProxy中继的令牌不是我需要用来验证/授权微服务A作为微服务B的客户端的令牌.

Spring Cloud参考文档目前说:"基于Spring Boot和Spring Security OAuth2,我们可以快速创建实现常见模式的系统,如单点登录,令牌中继和令牌交换." (http://cloud.spring.io/spring-cloud-security/spring-cloud-security.html)

我想在参考文档中使用"Token Exchange"它们意味着OAuth2的这个扩展的实现,在本规范中解释,这基本上是我需要的:https: //tools.ietf.org/html/draft-ietf- OAuth的令牌交换-03

正如我所说,我了解如何使用SSO和令牌中继,但我无法在参考文档中看到有关如何实现"令牌交换"的进一步说明.我也无法找到实现示例.

有谁知道在哪里可以找到更多信息或示例?

非常感谢!

spring spring-boot spring-cloud spring-oauth2

13
推荐指数
1
解决办法
3210
查看次数