spring security快速使用的三种方法

spring security快速使用的三种方法

1.配置文件

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

在配置文件中添加

spring.security.user.name=admin
spring.security.user.password=admin

2.配置类


@Configuration
public class MySecurityConfig extends WebSecurityConfigurerAdapter {

    //配置类
	 @Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {

			BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
				 //加密
			  String password = passwordEncoder.encode("ali123");
			 //验证
			   auth.inMemoryAuthentication().withUser("ali").password(password).roles("admin");
			  }
			  
    @Bean
    PasswordEncoder password(){
        return new BCryptPasswordEncoder();
    }


}

3.链接数据库

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


        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.5</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
# 数据库配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/guli?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root

创建Users类

@Data
@TableName("acl_user")
public class Users {
    private Integer id;
    private String username;
    private String password;
}

创建配置文件

@Configuration
public class MySecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserDetailsService userDetailsService;
    //链接数据库
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(password());
    }
    @Bean
    PasswordEncoder password(){
        return new BCryptPasswordEncoder();
    }
}

创建service


@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService {


    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {

        QueryWrapper<Users> usersQueryWrapper = new QueryWrapper<>();
        usersQueryWrapper.eq("username",userName);
        Users users = userMapper.selectOne(usersQueryWrapper);

        if (users == null){
            throw new UsernameNotFoundException("用户不存在");
        }

        System.out.println(users.toString());
        List<GrantedAuthority> auths = AuthorityUtils.commaSeparatedStringToAuthorityList("role");
        return new User(users.getUsername(),new BCryptPasswordEncoder().encode(users.getPassword()),auths);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值