OAuth2.0系列三:OAuth2.0密码模式

x33g5p2x  于2021-12-18 转载在 其他  
字(3.3k)|赞(0)|评价(0)|浏览(331)

密码模式

如果你高度信任某个第三方应用,可以把用户名和密码告诉这个第三方应用,第三方应用拿到用户名和密码去申请授权获取令牌。在这种模式中,用户必须把自己的密码给客户端,但是客户端不得储存密码。这通常用在用户对客户端高度信任的情况下,比如客户端是操作系统的一部分,或者由一个著名公司出品。而认证服务器只有在其他授权模式无法执行的情况下,才能考虑使用这种模式。

  1. 用户向第三方客户端提供用户名和密码
  2. 第三方客户端将用户名和密码发给授权服务器,申请令牌
  3. 授权服务器确认无误后向第三方客户端发放令牌

第一步,创建maven项目password_server,pom文件如下

<parent>
        <artifactId>microservice-parent</artifactId>
        <groupId>com.curise.microservice</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>password_server</artifactId>
    <description>OAuth2.0密码模式</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- for OAuth 2.0 -->
        <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

    </dependencies>

第二步,创建授权管理器

@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationServer extends AuthorizationServerConfigurerAdapter {
    @Autowired
    private AuthenticationManager authenticationManager;

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        // 用户认证
        endpoints.authenticationManager(authenticationManager);
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                // 客户端id
                .withClient("app")
                // 客户端密钥
                .secret("123")
                // 权限
                .scopes("read","write")
                // 获取授权码后重定向地址
                .redirectUris("http://localhost:9000/callback")
                // 授权码和刷新token
                .authorizedGrantTypes("authorization_code","refresh_token")
                .and()
                .withClient("app1")
                .secret("1234")
                .scopes("read", "write")
                // 密码模式和刷新token
                .authorizedGrantTypes("password", "refresh_token");
    }
}

第三步,创建资源管理器

@Configuration
@EnableResourceServer
public class OAuth2ResourceServer extends ResourceServerConfigurerAdapter {
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest()
                .authenticated()
                .and()
                .requestMatchers()
                // /api/**请求需要OAuth鉴权
                .antMatchers("/api/**");
    }
}

第四步,创建配置文件

server.port=8080
security.user.name=root
security.user.password=root

第五步,创建Controller提供用户信息查询API

@RestController
public class UserController {

    @GetMapping("/api/userInfo")
    public ResponseEntity<UserInfo> getUserInfo(){
        User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String email = user.getUsername() + "@qq.com";

        UserInfo userInfo = new UserInfo();
        userInfo.setUsername(user.getUsername());
        userInfo.setEmail(email);
        return ResponseEntity.ok(userInfo);
    }

}

第六步,启动应用。

第七步,申请令牌。

通过如下请求:

localhost:8080/oauth/token?grant_type=password

                                           &username=root

                                           &password=root

                                           &scope=read

grant_type表示使用密码模式

username是用户名

password是密码

scope是权限范围

Header信息如下:

第八步,通过令牌获取用户信息

代码已经共享到GitHub,地址:https://github.com/WYA1993/microservice

参考文章和课程:

OAuth2.0标准 RFC6749

OAuth 2.0 的四种方式

OAuth2.0详解(授权模式篇)

《微服务架构实战160讲》——杨波

相关文章