Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 如何使用登录表单和github设置oauth2授权服务器?_Spring_Spring Boot_Spring Security_Oauth 2.0_Spring Security Oauth2 - Fatal编程技术网

Spring 如何使用登录表单和github设置oauth2授权服务器?

Spring 如何使用登录表单和github设置oauth2授权服务器?,spring,spring-boot,spring-security,oauth-2.0,spring-security-oauth2,Spring,Spring Boot,Spring Security,Oauth 2.0,Spring Security Oauth2,我正在尝试使用spring boot设置授权服务器 此服务器应该能够使用登录/密码表单和github(例如)对用户进行授权 我希望这张图片能帮助我更好地理解我想要什么 登录表单示例: 要求: 用户必须能够使用登录/密码表单直接登录(通过在浏览器中输入授权服务器的地址)到服务器 用户必须能够使用github授权直接登录到服务器 客户端(另一个应用程序)必须能够使用此授权服务器以登录/密码的形式对用户进行授权(获取访问\u令牌等…) 客户端(另一个应用程序)必须能够通过github登录按钮使用此授

我正在尝试使用spring boot设置授权服务器

此服务器应该能够使用登录/密码表单和github(例如)对用户进行授权

我希望这张图片能帮助我更好地理解我想要什么

登录表单示例:

要求:

  • 用户必须能够使用登录/密码表单直接登录(通过在浏览器中输入授权服务器的地址)到服务器
  • 用户必须能够使用github授权直接登录到服务器
  • 客户端(另一个应用程序)必须能够使用此授权服务器以登录/密码的形式对用户进行授权(获取访问\u令牌等…)
  • 客户端(另一个应用程序)必须能够通过github登录按钮使用此授权服务器对用户进行授权
  • 使用代码,我只能完成需求列表中的前3点:

    @SpringBootApplication
    @RestController
    @Configuration
    @Import(AuthorizationServerSecurityConfiguration.class)
    public class SsoApplication extends WebSecurityConfigurerAdapter {
    
        public static void main(String[] args) {
            SpringApplication.run(SsoApplication.class, args);
        }
    
        @GetMapping("/")
        public Authentication me() {
            return SecurityContextHolder.getContext().getAuthentication();
        }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                    .authorizeRequests()
                    .anyRequest().authenticated()
                    .and()
                    .formLogin()
                    .and()
                    .oauth2Login();
        }
    
        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication()
                    .withUser("123")
                    .password(passwordEncoder().encode("123"))
                    .roles("USER");
        }
    
        @Bean
        public BCryptPasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); }
    
    }
    
    完整的代码示例可以在

    作为检查项目3和4的客户端,您可以使用具有以下设置的邮递员来获取令牌:

    • 授权类型:授权代码
    • 回调URL:
    • 验证URL:
    • 访问令牌URL:
    • 客户端ID:第一个客户端
    • 客户秘密:NoonewillerGuess
    • 范围:用户信息
    • 状态:测试
    • 客户端身份验证:作为基本身份验证头发送

    我未能实施:

  • 客户端(另一个应用程序)必须能够通过github登录按钮授权使用此授权服务器的用户
  • 邮递员控制台出现错误:

    Invalid authorization code: 97477114b3ec3c53547d
    
    我将感谢任何帮助。
    谢谢

    你的代码对我来说很有用

    但我已经在github中创建了自己的“OAuth应用程序”。然后,使用您的代码,我在我的Github应用程序中添加了:

    网页网址:

    授权回调URL:

    在给出按钮并允许应用程序使用github识别我之后,我返回了主

    ...
    "authenticated": true,
    "principal": {
        "authorities": [
          {
            "authority": "ROLE_USER",
            "attributes": {
              "login": "xxx",
    ...
    
    正如您所看到的,spring应用程序不是问题所在,我认为是您的github配置

    编辑:

    在邮递员控制台中,如果将注释
    @enableAuth2sso
    放入配置
    SsoApplication
    ,则错误消失:

    @SpringBootApplication
    @RestController
    @Configuration
    @EnableOAuth2Sso
    @Import(AuthorizationServerSecurityConfiguration.class)
    public class SsoApplication extends WebSecurityConfigurerAdapter
    

    你的代码对我很有用

    但我已经在github中创建了自己的“OAuth应用程序”。然后,使用您的代码,我在我的Github应用程序中添加了:

    网页网址:

    授权回调URL:

    在给出按钮并允许应用程序使用github识别我之后,我返回了主

    ...
    "authenticated": true,
    "principal": {
        "authorities": [
          {
            "authority": "ROLE_USER",
            "attributes": {
              "login": "xxx",
    ...
    
    正如您所看到的,spring应用程序不是问题所在,我认为是您的github配置

    编辑:

    在邮递员控制台中,如果将注释
    @enableAuth2sso
    放入配置
    SsoApplication
    ,则错误消失:

    @SpringBootApplication
    @RestController
    @Configuration
    @EnableOAuth2Sso
    @Import(AuthorizationServerSecurityConfiguration.class)
    public class SsoApplication extends WebSecurityConfigurerAdapter
    

    我找到了错误的原因。问题是Postman从github截取了用于授权服务器的错误代码,而不是从客户端的授权服务器截取了所需的代码


    换句话说,问题出在邮递员身上。为了进行正确的验证,必须使用成熟的oauth2客户端。

    我找到了错误的原因。问题是Postman从github截取了用于授权服务器的错误代码,而不是从客户端的授权服务器截取了所需的代码

    换句话说,问题出在邮递员身上。为了进行正确的验证,必须使用成熟的oauth2客户机