基于Oauth2.0的单点登录—搭建认证服务器(四)

基于Oauth2.0的单点登录—初识Security(一)
基于Oauth2.0的单点登录—Oauth2.0(二)
基于Oauth2.0的单点登录—单点登录(三)
基于Oauth2.0的单点登录—搭建认证服务器(四)
基于Oauth2.0的单点登录—搭建资源服务器(五)
基于Oauth2.0的单点登录—搭建客户端(六)
单点登录示例代码传送门

一. 安全相关配置

继承WebSecurityConfigurerAdapter,如下代码所示。

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
   
}

(一). 用户信息配置

  1. 存储在内存中,有两种创建用户信息的方式:
    • 分别创建用户的用户名、密码、角色等信息;
    • 基于org.springframework.security.core.userdetails.User创建用户信息。
    @Configuration
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
         
    
        @Bean
        PasswordEncoder passwordEncoder(){
         
            return new BCryptPasswordEncoder();
        }
    
        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
         
            auth.inMemoryAuthentication()
                    .withUser("admin").password(passwordEncoder().encode("admin")).roles("ADMIN")
                    .and()
                    .withUser(User.builder().username("user").password(passwordEncoder().encode("user")).roles("USER").build())
                    .passwordEncoder(passwordEncoder());
        }
    }
    
  2. 存储在数据库中(使用默认表结构与实现)
    表结构创建文件:users.ddl(在IDEA中Ctrl+Shift+N搜索users.ddl),注意在mysql中需要将varchar_ignorecase替换为varchar,sql语句如下:
    create table users(username varchar(50) not null primary key,password varchar(500) not null,enabled boolean not null);
    create table authorities (username varchar(50) not null,authority varchar(50) not null,constraint fk_authorities_users foreign key(username) references users(username));
    create unique index ix_auth_username on authorities (username,authority);
    
    在代码中的相关配置,需要注入DataSource与PasswordEncoder。
    @Configuration
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
         
    
        @Autowired
        private DataSource dataSource;
    
        @Bean
        PasswordEncoder passwordEncoder(){
         
            return new BCryptPasswordEncoder();
        }
    
        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
         
            auth.jdbcAuthentication()
                    .passwordEncoder(passwordEncoder())
                    .dataSource(dataSource);
        }
    }
    
  3. 存储在数据库中(使用自定义表结构与实现)
    自定义User对象,需要实现UserDetails接口,同时可以实现属性的扩展,例如增加手机号等信息。
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Entity(name = "my_user")
    public class MyUser implements UserDetails {
         
        /**
         * 主键ID
         */
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Integer userId;
    
        /**
         * 用户名
         */
        private String username;
    
        /**
         * 密钥
         */
        private String password;
    
        /**
         * 手机号
         */
        private String telNo;
    
        /**
         * 拥有角色
         */
        @ManyToMany(fetch = FetchType.EAGER)
        private List<MyUserRole> authorities;
    
        @Override
        public boolean isAccountNonExpired() {
         
            return true;
        }
    
        @Override
        public boolean isAccountNonLocked() {
         
            return true;
        }
    
        @Override
        public boolean isCredentialsNonExpired() {
         
            return true;
        }
    
        @Override
        public boolean isEnabled() {
         
            return true;
        }
    }
    
    自定义Role对象,需要实现GrantedAuthority接口。
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Entity(name = "my_user_role")
    public class MyUserRole implements GrantedAuthority {
         
        /**
         * 主键ID
         */
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Integer roleId;
    
        /**
         * 角色
         */
        private String authority;
    }
    
    自定义UserDetailsService的实现类,并实现loadUserByUsername()方法。
    public interface IMyUserDAO 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值