标签: spring-oauth2

Spring Oauth2 - 重新加载校长

我已经使用spring安全模块实现了OAuth2密码授权.我添加了自己的UserDetails实现和UserDetailsS​​ervice(jdbc).我将用户注入我的控制器:

@AuthenticationPrincipal User user
Run Code Online (Sandbox Code Playgroud)

User是UserDetails的实现.现在我想添加更改用户数据的可能性而不刷新令牌.

我尝试用以下内容刷新主体:

User updatedUser = ...
Authentication newAuth = new UsernamePasswordAuthenticationToken(updatedUser, updatedUser.getPassword(), updatedUser.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(newAuth);
Run Code Online (Sandbox Code Playgroud)

但它不起作用,当我调用另一个控制器方法时,它返回旧的User对象.

有没有办法在没有刷新令牌的情况下更改用户数据?是否有任何解决方案使Spring安全性始终从数据库(而不是从Cache)加载用户数据?

spring-security spring-oauth2

9
推荐指数
1
解决办法
940
查看次数

如何使用Postman Authentication帮助程序调用JHipster(Spring)OAuth2 Rest服务器

Postman有身份验证帮助程序来帮助进行身份验证调用,我正在尝试使用OAuth 2.0帮助程序使用Spring(安全,社交等)调用由JHipster创建的REST服务器.

我尝试了很多配置,这是屏幕(客户端ID和秘密被屏蔽):

Auth帮助程序配置

对于我尝试过的授权URL:

我从收到令牌回到邮递员的距离越近:

响应失败

我不知道为什么这样犯错.也许我错误地设置了回拨网址?我是否需要在服务器或客户端(AngularJS)中执行此操作?

有没有人知道什么是错的?我感谢您的帮助.

spring-security spring-security-oauth2 jhipster postman spring-oauth2

9
推荐指数
2
解决办法
7278
查看次数

Spring Oauth2如何登录重定向工作?

我一直在使用Spring Boot Oauth2教程捣乱,我似乎无法获得一个非常关键的元素:

https://spring.io/guides/tutorials/spring-boot-oauth2/

我想作为授权服务器运行.我尽可能地按照说明进行操作,但是当我进入/ oauth/authorize端点时,我得到的只是403 Forbidden响应.考虑到教程设置的HttpSecurity配置,这实际上对我有意义:

protected void configure(HttpSecurity http) throws Exception {
    http
      .antMatcher("/**")
      .authorizeRequests()
        .antMatchers("/", "/login**", "/webjars/**")
        .permitAll()
      .anyRequest()
        .authenticated()
        .and().logout().logoutSuccessUrl("/").permitAll()
        .and().csrf().csrfTokenRepository(csrfTokenRepository())
        .and().addFilterAfter(csrfHeaderFilter(), CsrfFilter.class)
        .addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class);
}
Run Code Online (Sandbox Code Playgroud)

本教程的登录页面实际上是主索引,我绝对没有在教程中看到任何指示Oauth系统重定向登录流程的内容.

我可以通过添加以下内容来实现它:

        .and().formLogin().loginPage("/")
Run Code Online (Sandbox Code Playgroud)

...但在继续前进之前,我真的很想了解这是教程中的问题还是我对它的实现或其他问题.Oauth安全系统决定"登录"页面的机制是什么?

spring spring-security spring-security-oauth2 spring-oauth2

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

TokenEndpoint:处理空指针异常

我已经尝试通过curl执行此命令从我的oauth2服务器请求代码

 curl -X POST -k -vu clientapp:123456 http://localhost:8080/oauth/token -H "Accept: application/json" -d "grant_type=authorization_code&scope=read%20write&client_secret=123456&client_id=clientapp&code=appcode&redirect_uri=localhost:3000"
Run Code Online (Sandbox Code Playgroud)

回应是

* Adding handle: conn: 0x608860
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x608860) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 8080 (#0)
*   Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
* Server auth using Basic with user 'clientapp'
> POST /oauth/token HTTP/1.1
> Authorization: Basic Y2xpZW50YXBwOjEyMzQ1Ng==
> User-Agent: …
Run Code Online (Sandbox Code Playgroud)

java spring spring-security spring-oauth2 oauth2

9
推荐指数
1
解决办法
3355
查看次数

需要身份验证才能获取访问令牌(匿名不允许)

我尝试修改现有的示例 - Tonr2和Sparklr2.我还根据Spring Boot Spring Boot OAuth2查看了本教程.我尝试在Tonr2示例中构建应用程序但没有首次登录(在tonr2上).我只需要在Sparklr2端进行一次身份验证.我这样做:

@Bean
    public OAuth2ProtectedResourceDetails sparklr() {
        AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails();
        details.setId("sparklr/tonr");
        details.setClientId("tonr");
        details.setTokenName("oauth_token");
        details.setClientSecret("secret");
        details.setAccessTokenUri(accessTokenUri);
        details.setUserAuthorizationUri(userAuthorizationUri);
        details.setScope(Arrays.asList("openid"));
        details.setGrantType("client_credentials");
        details.setAuthenticationScheme(AuthenticationScheme.none);
        details.setClientAuthenticationScheme(AuthenticationScheme.none);
        return details;
    }
Run Code Online (Sandbox Code Playgroud)

但我有Authentication is required to obtain an access token (anonymous not allowed).我查了这个问题.当然,我的用户是匿名的 - 我想登录Sparklr2.此外,我尝试了这种bean的不同设置组合,但没有什么好处.怎么解决?如何使它按我想要的方式工作?

java spring spring-mvc spring-security spring-oauth2

8
推荐指数
1
解决办法
2760
查看次数

使用Spring OAuth在Cookie中存储来自OAuth2服务器的令牌

Spring OAuth2是否提供了使用opaque或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

Run Code Online (Sandbox Code Playgroud)

spring-security spring-oauth2

8
推荐指数
1
解决办法
8768
查看次数

使用linkedIn访问令牌时缺少client_id

我已经扩展了spring security oauth示例social-auth-server,linkedIn被添加为第三个身份验证选项.托管授权服务器的教程中介绍了此项目.

源代码:https://github.com/spring-guides/tut-spring-boot-oauth2/tree/master/auth-server

如何配置此客户端以添加client_id?

LinkedIn属性:

linkedin:
  client:
    clientId: XXX
    clientSecret: YYY
    accessTokenUri: https://www.linkedin.com/oauth/v2/accessToken
    userAuthorizationUri: https://www.linkedin.com/oauth/v2/authorization
  resource:
    userInfoUri: https://api.linkedin.com/v1/people/~?format=json
Run Code Online (Sandbox Code Playgroud)

日志:

uth2ClientAuthenticationProcessingFilter : Request is to process authentication
 g.c.AuthorizationCodeAccessTokenProvider : Retrieving token from https://www.linkedin.com/oauth/v2/accessToken
 g.c.AuthorizationCodeAccessTokenProvider : Encoding and sending form: {grant_type=[authorization_code], code=[XXXXXXXXXXXXXXXXXXXX], redirect_uri=[http://localhost:8080/login/linkedin]}
 uth2ClientAuthenticationProcessingFilter : Authentication request failed: org.springframework.security.authentication.BadCredentialsException: Could not obtain access token
Run Code Online (Sandbox Code Playgroud)

例外:

org.springframework.security.authentication.BadCredentialsException: Could not obtain access token
    at org.springframework.security.oauth2.client.filter.OAuth2ClientAuthenticationProcessingFilter.attemptAuthentication(OAuth2ClientAuthenticationProcessingFilter.java:107) ~[spring-security-oauth2-2.0.13.RELEASE.jar:na]
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:212) ~[spring-security-web-4.2.2.RELEASE.jar:4.2.2.RELEASE]
    at org.springframework.web.filter.CompositeFilter$VirtualFilterChain.doFilter(CompositeFilter.java:112) [spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.web.filter.CompositeFilter.doFilter(CompositeFilter.java:73) [spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at …
Run Code Online (Sandbox Code Playgroud)

spring oauth spring-security spring-oauth2

8
推荐指数
1
解决办法
899
查看次数

如何从 Spring 授权服务器示例获取刷新令牌

官方示例 Spring Authorization Server默认为带有 PKCE 的 Oauth 2.1返回access_tokenid_token

https://github.com/spring-projects/spring-authorization-server/tree/main/samples/default-authorizationserver

端点/oauth2/token是否有可能也在响应中返回刷新令牌?为了获取刷新令牌,我需要在示例中进行哪些更改或配置?

这是邮递员对令牌的请求 在此输入图像描述

我还将提到我必须使用 PKCE 对代码流进行的一些更改

禁用 CSRF

http
    .authorizeRequests(authorizeRequests ->
        authorizeRequests.anyRequest().authenticated()
    )
    .formLogin(withDefaults())
    .csrf().disable();
Run Code Online (Sandbox Code Playgroud)

将 ClientAuthenticationMethod.CLIENT_SECRET_BASIC 更改为 ClientAuthenticationMethod.NONE

将 requireAuthorizationConsent(true) 更改为 requireProofKey(true)

spring authorization spring-security spring-oauth2 spring-authorization-server

8
推荐指数
1
解决办法
5814
查看次数

如何自动装配这个TokenStore

如何触发此示例Spring Boot OAuth2应用程序的自动注销?

我尝试将以下代码从另一个帖子的答案中添加到应用程序demo包中的新控制器类中: authserver

package demo;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.http.HttpStatus;

@Controller
public class OAuthController {
    @Autowired
    private TokenStore tokenStore;

    @RequestMapping(value = "/oauth/revoke-token", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public void logout(HttpServletRequest request) {
        String authHeader = request.getHeader("Authorization");
        if (authHeader != null) {
            String tokenValue = authHeader.replace("Bearer", "").trim();
            OAuth2AccessToken accessToken = tokenStore.readAccessToken(tokenValue);
            tokenStore.removeAccessToken(accessToken);
        }
    }
}  
Run Code Online (Sandbox Code Playgroud)

但是当我尝试启动应用程序时,调试日志会显示以下错误,指示它不能autowire存储令牌存储:

Caused by: org.springframework.beans.factory.BeanCreationException: …
Run Code Online (Sandbox Code Playgroud)

spring spring-security autowired spring-boot spring-oauth2

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

Spring Security HttpSecurity 配置

我尝试了解 RequestMatcher、AntMatcher 等是如何工作的。我阅读了一些帖子并了解了基础知识。实际上我有这个简单的基本配置:

\n\n
@Override\nprotected void configure(HttpSecurity http) throws Exception {\n    http.requestMatchers() //1\n        .antMatchers("/login", "/oauth/authorize") //2\n        .and() //3\n        .authorizeRequests() //4\n        .anyRequest() //5\n        .authenticated() //6;\n
Run Code Online (Sandbox Code Playgroud)\n\n

我真的不明白第 1,2 和 3 点。根据我的理解,这意味着/login和的请求/oauth/authorize请求被映射并且应该是授权请求。所有其他请求都需要进行身份验证。

\n\n

对于端点来说,/user/me我必须进行身份验证,因为它由第 5 点和第 6 点规定?\n对此端点的调用对我有用。

\n\n

在我的其他配置中,我尝试了一种不同的方法:

\n\n
@Override\nprotected void configure(HttpSecurity http) throws Exception { // @formatter:off\n      http\n       .authorizeRequests() //1\n        .antMatchers("/login", "/oauth/authorize", "/img/**").permitAll() //2\n        .anyRequest() //3\n        .authenticated() //4\n
Run Code Online (Sandbox Code Playgroud)\n\n

从我的角度来看,这应该与第一个配置的逻辑相同。但实际上端点/user/me不再可访问。

\n\n

我非常感谢您的澄清

\n\n
\n\n

更新1:

\n\n

这是我现在的配置:

\n\n
@Override\nprotected void configure(HttpSecurity …
Run Code Online (Sandbox Code Playgroud)

java spring spring-security spring-boot spring-oauth2

7
推荐指数
1
解决办法
6082
查看次数