您的位置:首页 > 其它

【最简OAuth2系列教程】开发认证中心模块

2021-03-03 21:58 274 查看

背景:网上很多讲配置 oauth2 ,配置方法 复杂纷繁对于初学者很不友好,让人望而却步

欢迎关注本系列博客 基于 spring cloud 最新版本 hoxton 完成oauth2 的实践

  • 基于 

    SpringCloudOAuth
    ,用简洁的方式搭建oauth的认证中心,

  • 关于oauth2 的授权模式 请直接参考 阮一峰 OAuth 2.0 的四种方式的详细介绍

  • 项目版本核心说明

名称版本
Spring Boot2.2.0.M5
Spring CloudHoxton.M2
Spring Cloud OAuth22.2.0.M2

开始配置

maven 依赖引入

  • 这里只需要引入web、 cloud-oauth 即可,暂不引入spring cloud 其他组件

  1. <dependencies>

  2.    <dependency>

  3.        <groupId>org.springframework.boot</groupId>

  4.        <artifactId>spring-boot-starter-web</artifactId>

  5.    </dependency>


  6.    <dependency>

  7.        <groupId>org.springframework.cloud</groupId>

  8.        <artifactId>spring-cloud-starter-oauth2</artifactId>

  9.    </dependency>

  10. </dependencies>

配置web安全,拦截全部的请求

  • 获取web 上下文AuthenticationManager 注入到spring中,方便后边oauth server注入

  • 创建UserDetailsService的内存实现,注入一个测试用户

  1. @Configuration

  2. @EnableWebSecurity

  3. @EnableGlobalMethodSecurity(prePostEnabled = true)

  4. public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

  5.    /**

  6.     * 必须注入 AuthenticationManager,不然oauth  无法处理四种授权方式

  7.     *

  8.     * @return

  9.     * @throws Exception

  10.     */

  11.    @Bean

  12.    @Override

  13.    public AuthenticationManager authenticationManagerBean() throws Exception {

  14.        return super.authenticationManagerBean();

  15.    }


  16.    /**

  17.     * 必须注入UserDetailsService ,不然oauth  密码模式等死循环问题

  18.     *

  19.     * @return

  20.     */

  21.    @Bean

  22.    @Override

  23.    protected UserDetailsService userDetailsService() {

  24.        InMemoryUserDetailsManager userDetailsManager = new InMemoryUserDetailsManager();

  25.        userDetailsManager.createUser(User.withUsername("lengleng").password("{noop}lengleng").authorities("USER").build());

  26.        return userDetailsManager;

  27.    }

  28. }

配置oauth2 认证服务器

  • 配置clientId 信息,及其支持的授权模式,特别注意这里是五种包含一个刷新操作

  1. @Configuration

  2. @EnableAuthorizationServer

  3. public class BigAuthServerConfiguration extends AuthorizationServerConfigurerAdapter {

  4.    @Autowired

  5.    private AuthenticationManager authenticationManager;

  6.    @Autowired

  7.    private UserDetailsService userDetailsService;


  8.    @Override

  9.    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

  10.        clients.inMemory()

  11.                .withClient("appid")

  12.                .secret("{noop}secret")

  13.                .authorizedGrantTypes("password", "authorization_code", "client_credentials", "implicit", "refresh_token")

  14.                .scopes("all");

  15.    }


  16.    @Override

  17.    public void configure(AuthorizationServerEndpointsConfigurer endpoints) {

  18.        endpoints.authenticationManager(authenticationManager)

  19.                .userDetailsService(userDetailsService);

  20.    }


  21. }


以上完成了认证服务器的功能

测试密码模式

curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'grant_type=password&username=lengleng&password=lengleng&scope=all' "http://appid:secret@localhost:8764/oauth/token"

总结

  • 关于spring security 密码处理 {noop}密码


    spring security 5 后支持密码动态加密, {noop}密码 spring security 则去找NoOpPasswordEncoder解密,不需要自定义 PasswordEncoder 保证安全性,支持如下


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: