Tag: spring security oauth2

Spring Security OAuth 2,带有表单登录

我正在尝试配置我的Spring启动应用程序以使用表单登录,并使用OAuth 2授权服务器validation凭据(将表单登录中的凭据发送到用户授权URL。 但是,当我使用以下SecurityConfig并且我转到资源时,而不是使用表单登录,它会重定向到授权服务器,询问我的凭据(使用基本身份validation),然后重定向回应用程序本身。 我正在使用以下SecurityConfig : @Configuration @EnableOAuth2Sso public class SecurityConfig extends OAuth2SsoConfigurerAdapter { @Override public void configure(HttpSecurity http) throws Exception { http .logout() .and() .antMatcher(“/**”) .authorizeRequests() .anyRequest().authenticated() .and() .csrf() .csrfTokenRepository(csrfTokenRepository()).and() .addFilterAfter(csrfHeaderFilter(), CsrfFilter.class) .formLogin(); } // CSRF repository + filter } 我将formLogin()提供给configure()方法,但这似乎不起作用。 以下配置位于我的application.yml文件中: spring: oauth2: client: clientId: test clientSecret: secret accessTokenUri: http://localhost:8081/uaa/oauth/token userAuthorizationUri: http://localhost:8081/uaa/oauth/authorize clientAuthenticationScheme: header […]

Spring-Security-Oauth2:默认登录成功url

是否可以为Spring Oauth2 Sso服务设置默认登录成功? 继szenario之后 浏览器reqeuests index.html sso服务:不受保护==>返回index.html index.html包含manifest属性==>浏览器请求清单 sso服务:Manifest受保护==>返回401 客户端重定向到${sso.host}/login sso服务重定向到auth服务器 authentication ==>使用query-String中的代码重定向回${sso.host}/login sso服务:请求令牌并重定向到清单文件 有没有办法不重定向到受保护的最后一个请求的资源,但默认情况下重定向到’index.html’? 即使没有办法实现这一点,请告诉我

如何在Spring Boot应用程序上启用Bearer身份validation?

我想要实现的是: 存储在通过jdbc访问的数据库(即MySQL)中的用户,权限,客户端和访问令牌 API公开了端点,让您问“我可以拥有OAuth2不记名令牌吗?我知道客户端ID和密码” 如果您在请求标头中提供Bearer令牌,则API允许您访问MVC端点 我对此非常了解 – 前两点正在发挥作用。 我无法为我的Spring Boot应用程序使用完全默认的OAuth2设置,因为标准表名已经在我的数据库中使用(例如,我已经有一个“用户”表)。 我手动构建了自己的JdbcTokenStore,JdbcClientDetailsS​​ervice和JdbcAuthorizationCodeServices实例,将它们配置为使用我的数据库中的自定义表名,并设置我的应用程序以使用这些实例。 所以,这就是我到目前为止所拥有的。 我可以要求持票人令牌: # The `-u` switch provides the client ID & secret over HTTP Basic Auth curl -u8fc9d384-619a-11e7-9fe6-246798c61721:9397ce6c-619a-11e7-9fe6-246798c61721 \ ‘http://localhost:8080/oauth/token’ \ -d grant_type=password \ -d username=bob \ -d password=tom 我收到回复; 太好了! {“access_token”:”1ee9b381-e71a-4e2f-8782-54ab1ce4d140″,”token_type”:”bearer”,”refresh_token”:”8db897c7-03c6-4fc3-bf13-8b0296b41776″,”expires_in”:26321,”scope”:”read write”} 现在我尝试使用该令牌: curl ‘http://localhost:8080/test’ \ -H “Authorization: Bearer 1ee9b381-e71a-4e2f-8782-54ab1ce4d140” 唉: { “timestamp”:1499452163373, “status”:401, […]

Spring Boot OAuth2在1.4.1版上无法正常运行

我在Spring OAuth2上使用Spring Boot 1.4.0。 当我请求令牌时,服务器响应是: { “access_token”: “93f8693a-22d2-4139-a4ea-d787f2630f04”, “token_type”: “bearer”, “refresh_token”: “2800ea24-bb4a-4a01-ba87-2d114c1a2235”, “expires_in”: 899, “scope”: “read write” } 当我将项目更新到Spring Boot 1.4.1时,服务器响应变为 { “error”: “invalid_client”, “error_description”: “Bad client credentials” } 什么从版本1.4.0更改为1.4.1? 我应该怎样做才能让我的请求再次发挥作用? 编辑 WebSecurityConfiguration: @Configuration @EnableWebSecurity public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter{ /** The client details service. */ @Autowired private ClientDetailsService clientDetailsService; /** The password encoder. */ […]

如何在TokenEndPoint Spring oAuth2中注入WebResponseExceptionTranslator

我试图自定义Spring Security oAuth TokenEndpoint类处理exception的方式。 我想返回一个比DefaultWebResponseExceptionTranslator当前更自定义的JSON响应。 我无法弄清楚如何通过XML注入自定义转换器。 这是我的代码片段: 弹簧security.xml文件 这是AbstractServerBeanDefinitionParser的相关代码部分 BeanDefinitionBuilder tokenEndpointBean = BeanDefinitionBuilder.rootBeanDefinition(TokenEndpoint.class); tokenEndpointBean.addPropertyReference(“clientDetailsService”, clientDetailsRef); tokenEndpointBean.addPropertyReference(“tokenGranter”, tokenGranterRef); authorizationEndpointBean.addPropertyReference(“oAuth2RequestValidator”, oAuth2RequestValidatorRef); parserContext.getRegistry() .registerBeanDefinition(“oauth2TokenEndpoint”, tokenEndpointBean.getBeanDefinition()); if (StringUtils.hasText(oAuth2RequestFactoryRef)) { tokenEndpointBean.addPropertyReference(“oAuth2RequestFactory”, oAuth2RequestFactoryRef); } if (StringUtils.hasText(oAuth2RequestValidatorRef)) { tokenEndpointBean.addPropertyReference(“oAuth2RequestValidator”, oAuth2RequestValidatorRef); } TokenEndpoint扩展了AbstractEndpoint,它是定义转换器的地方。 @FrameworkEndpoint public class TokenEndpoint extends AbstractEndpoint { @ExceptionHandler(Exception.class) public ResponseEntity handleException(Exception e) throws Exception { logger.info(“Handling error: ” + e.getClass().getSimpleName() […]

如何包装OAuth2exception?

我们有一个使用Spring OAuth2的rest API。 用户通过身份validation后,所有JSON响应都采用以下格式: {“code” : 12345, “data” : “…” } 但是,身份validation失败的JSON响应与上述格式不一致,因为它由Spring处理。 例如,如果凭据不正确,客户端将获得带有JSON响应的HTTP状态代码400,如下所示: {“error”: “invalid_grant”, “error_description”: “Bad credentials” } 如果用户帐户被锁定,客户端将获得具有JSON响应的HTTP状态代码400,如下所示 {“error”:”invalid_grant”,”error_description”:”User account is locked”} 所有这一切都是因为Spring TokenEndpoint.handleException()正在处理与/ oauth / token相关的exception 我想更改OAuth2失败的JSON响应以遵循第一种格式。 这是我迄今为止尝试过的,没有成功: 使用具有最高优先级顺序的ControllerAdvice并使用此处所述的@ExceptionHandler 如此处所述实现OAuth2ExceptionRenderer 实现ExceptionMapper 添加了一个新的ObjectMapper,扩展了StdSerializer。 虽然我的objectmapper已初始化,但它不用于序列化exception。 也许是因为Spring直接调用MappingJackson2HttpMessageConverter而且我的应用程序中似乎有这个类的几个实例。 任何上述方法或新方法的任何帮助将受到高度赞赏。 我没有尝试过这种方法,因为我无法更改现有客户端的上下文路径。

使用OAuth2和JWT的Spring Security:编码的密码看起来不像BCrypt

我正在尝试使用JWT实现Spring AuthorizationServer。 我能够生成JWT令牌并登录,直到我将BCrypt添加到混音中。 现在,当我尝试登录时,我从API获得“错误凭据”。 OAuth2Configuration.java @Configuration @EnableAuthorizationServer public class OAuth2Configuration extends AuthorizationServerConfigurerAdapter { private DataSource dataSource; private AuthenticationManager authenticationManager; private BCryptPasswordEncoder passwordEncoder; public OAuth2Configuration(AuthenticationManager authenticationManager) { this.authenticationManager = authenticationManager; this.dataSource = new Jdbc3PoolingDataSource(); this.passwordEncoder = new BCryptPasswordEncoder(); } @Override public void configure(AuthorizationServerSecurityConfigurer security) throws Exception { security.passwordEncoder(passwordEncoder); } @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception […]

可以/应该使用spring security中的每个请求刷新OAuth2令牌

我们使用username-password grant将我们的JS客户端连接到我们的REST服务器。 在某种程度上,oauth / token返回的令牌是我们的会话,因为它允许在有限的时间内访问后端。 每当我们使用令牌向后端发出请求时,我们都想刷新该会话/令牌。 我知道服务器发出了这个刷新令牌,我可以使用它在令牌过期后刷新令牌。 问题是:我不想让客户端负责捕获令牌过期的exception,并在令牌过期之前重新validation或安排刷新。 我希望令牌能够刷新,直到它在有限的时间内不再使用 – 就像会话一样。 (我也不希望它发出每个“数据”请求的刷新请求,虽然我想我记得读过,刷新令牌只有一次…?!) 有没有办法在spring security中做到这一点,还是我必须构建一些自定义的令牌存储实现或我选择的任何部分? 因为我无法找到答案(因此是post)我在想:也许这样做是不明智的,尽管我不明白为什么。 如果我可以窃取令牌,我也可以窃取刷新令牌。 所以我想我真的没有看到首先有一个刷新令牌的重点.. 编辑 为了回应Luke Taylor的回答,我将澄清我们的用例。 我们有一个REST服务器,可以保存人员等应用程序数据。 但也提供访问我们的内容管理,并允许客户发布到Facebook。 它封装了应用程序逻辑和数据存储 我们有一个完全成熟的客户端应用程序,它有自己的安全层,只是通过客户端凭证流访问我们的REST服务器上的数据。 谁可以做客户端决定的事情 我们有几个中小型应用程序,比如facebook上的联系人应用程序,它们也使用客户端凭据访问REST服务器上的数据 我们现在正在开发一个客户端应用程序,它只使用javascript访问REST层来完成大客户端应用程序所做的所有工作,但还需要提供一种方法来validation单个用户并允许多租户。 因此,此新客户端应用程序使用用户名密码授予进行身份validation,并使用方法级别安全性来授权用户 因此,我们有一个REST服务器,需要提供对我们的可信应用程序的完全访问权限,该应用程序执行自己的安全性工作,同一服务器需要为我们的新多租户javascript客户端应用程序的用户提供访问权限。 在生产中,我们将有几个REST服务器,每个服务器都有自己的数据库,但核心总是相同的,所以理论上一个服务器应该能够处理所有。

使用JDBC和JWT实现Spring OAuth2,并使用基于XML的配置自定义现有授权流

我开始使用Spring OAuth2,在这个过程中,我很难找到相关的教程和内容,主要是因为以下内容 我不想使用Spring Boot 我不想使用Java配置而是使用xml配置 我需要根据我们的特定需求自定义Spring OAuth2授权流程和其他function 我需要禁用一些授权流程 我需要Spring OAuth2来使用自定义用户和角色 我需要在自定义数据库选项卡中使用store oauth_client详细信息 其他的东西 我设法编写了我的实现,解决了上述问题,现在我想分享我的发现,以便拯救他人的痛苦。 请参阅我的答案,了解我所采用的方法,如果您有任何建议,请随时分享您的建议,建议和反馈。 这个问题的主要目标是 获得有关我遵循的方法的反馈,建议和建议 分享我所学到的所有困难,希望能够拯救他人的麻烦,并回馈社区从社区中学到的东西。

Spring-boot oauth2拆分授权服务器和资源服务器

我试图在spring-boot中将资源服务器从授权服务器中分离出来。 我有两个不同的应用程序,我分开运行。 在授权服务器中,我可以从oauth / token获取承载令牌,但是当我试图访问资源(在头部中发送令牌)时,我收到了无效的令牌错误。 我的目的是使用InMemoryTokenStore和承载令牌。 谁能告诉我我的代码有什么问题? 授权服务器: @SpringBootApplication public class AuthorizationServer extends WebMvcConfigurerAdapter { public static void main(String[] args) { SpringApplication.run(AuthorizationServer.class, args); } @Configuration @EnableAuthorizationServer protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter { private TokenStore tokenStore = new InMemoryTokenStore(); @Autowired private AuthenticationManager authenticationManager; @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints .authenticationManager(authenticationManager) .tokenStore(tokenStore); } […]