SpringSecurity02


Web权限

1.认证

   (1)设置账号和密码

      一.通过配置文件 application.properties

spring.security.user.name=atguigu
spring.security.user.password=atguigu

   二.通过配置类 SecurityConfig

   

  @Override
    protected void  configure(AuthenticationManagerBuilder auth) throws Exception{

        //加密密码
        BCryptPasswordEncoder passwordEncode=new BCryptPasswordEncoder();
        String password = passwordEncode.encode("123");

        //设置用户名和密码
        auth.inMemoryAuthentication().withUser("luck").password(password).roles("admin");

    }

    @Bean
    PasswordEncoder password(){
        return  new BCryptPasswordEncoder();
    }

三.自定义实现类

(1)创建配置类,设置使用哪个userDetailsService实现类

@Configuration
public class SecurityConfigTest 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();
    }

}

(2)编写实现类,返回User对象, User对象有用户名密码和操作权限

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

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


        List auths = AuthorityUtils.commaSeparatedStringToAuthorityList("role");

        return new User("marry",new BCryptPasswordEncoder().encode("123"),auths);
    }
}

 (2)整合数据库

   整合MyBatilsPlus完成数据库操作

  1.添加依赖

        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.0.5
        
mysql mysql-connector-java
org.projectlombok lombok

2.创建数据库表

3.创建实体类

import lombok.Data;

@Data
public class Users {
    private Integer id;
    private String username;
    private String password;
}

4.整合MyBatilsPlus,创建接口,继承MyBatilsPlus的接口

public interface UsersMapper extends BaseMapper {

}

5.在MyUserDetailsService调用mapper里面的方法查询数据库进行认证

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

    @Autowired
    private UsersMapper usersMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //调用userMapper方法,根据用户名查询数据库
        
        QueryWrapper wrapper =new QueryWrapper<>();
        wrapper.eq("username",username);

        Users users = usersMapper.selectOne(wrapper);

        //判断
        if (users == null){//数据库没有用户名,认证失败
            throw new UsernameNotFoundException("用户名不存在!");
        }

        List auths = AuthorityUtils.commaSeparatedStringToAuthorityList("role");
         //从查询数据库返回users对象,得到用户名和密码,返回
        return new User(users.getUsername(),new BCryptPasswordEncoder().encode(users.getPassword()),auths);
    }
}

6.在启动类添加注解MapperScan

@SpringBootApplication
@MapperScan("com.mapper")
public class DemoApplication {

    public static void main(String[] args) {

        ConfigurableApplicationContext run = SpringApplication.run(DemoApplication.class, args);
    }

}

7.配置数据库驱动

spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/demo?characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver