Bootstrap

SpringBoot Security 自定义用户认证

通过自定义WebSecurityConfigurerAdapter类型的Bean组件,并重写configure(AuthenticationManagerBuilder auth)方法可以自定义用户认证。针对自定义用户认证,SpringSecurity提供了多种自定义认证方式,包括有:In-Memory Authentication(内存身份认证)、JDBC Authentication(JDBC身份认证)、LDAP Authentication(LDAP身份认证)、AuthenticationProviderr(身份认证提供商)和UserDetailsService(身份详情服务)。

实现三种身份认证:

In-Memory Authentication(内存身份认证)

In-Memory Authentication(内存身份认证)是最简单的身份认证方式,主要用于Security安全认证体验和测试。自定义内存身份认证是,只需要在重写的configure(AuthenticationManagerBuilder auth)方法中定义测试用户即可。下面将实现Spring boot整合Spring Security 实现自定义内存身份认证的实现。

1.自定义WebSecurityConfigurerAdapter配置类

1. 创建包和类

2.去继承WebSecurityConfigurerAdapter这个类,并且添加@EnableWebSecurity这个注解

@EnableWebSecurity  //开启MVC Security安全支持
public class SecurityConfig extends WebSecurityConfigurerAdapter {
}

 @EnableWebSecurity注解是一个组合注解,主要包括:@Configuration@Import({WebSecurity.class, SpringWebMvcImportSelector.class})@EnableGlobalAuthentication其中:

        @Configuration:是将当前自定义的SecurityConfig类作为Spring boot的配置类

        @Import:是根据pom.xml中导入的Web模块和Security模块进行自动化配置

        @EnableGlobalAuthentication:用于开启自定义的全局认证

注:如果是针对于Spring WebFlux框架的安全支持,需要在项目中导入Reactive Web模块和Security模块,并使用@EnableWebFluxSecurity注解开启基于WebFlux Security的安全支持

2.使用内存进行身份认证

在自定义的SecurityConfig类中重写configure(AuthenticationManagerBuilder auth)方法,并在该方法中使用内存身份认证的方式进行自定义用户认证:

package com.config;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.authentication.configurers.provisioning.InMemoryUserDetailsManagerConfigurer;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@EnableWebSecurity  //开启MVC Security安全支持
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //设置密码编码器
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        InMemoryUserDetailsManagerConfigurer<AuthenticationManagerBuilder> passwordEncoder = auth.inMemoryAuthentication().passwordEncoder(bCryptPasswordEncoder);
        //模拟两个测试用户
        passwordEncoder.withUser("xiaofen").password(bCryptPasswordEncoder.encode("123")).roles("comment");
        passwordEncoder.withUser("张三").password(bCryptPasswordEncoder.encode("321")).roles("vip");

    }
}
3.效果测试

现在只需要使用创建的模拟用户即可登录成功

JDBC Authentication(JDBC身份认证)

1.创建三个表

2.在pom.xml中,添加JDBC连接数据库的依赖启动器
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<scope>runtime</scope>
</dependency>
 3.进行数据库连接配置
spring.datasource.url=jdbc:mysql://localhost:3306/springbootdata?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
 4.跟内存身份认证差不多,需要继承WebSecurityConfigurerAdapter这个类。
package com.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.authentication.configurers.provisioning.InMemoryUserDetailsManagerConfigurer;
import org.springframework.security.config.annotation.authentication.configurers.provisioning.JdbcUserDetailsManagerConfigurer;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.sql.DataSource;

@EnableWebSecurity  //开启MVC Security安全支持
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Qualifier("dataSource")
    @Autowired
    private DataSource dataSource;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //设置密码编码器
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        InMemoryUserDetailsManagerConfigurer<AuthenticationManagerBuilder> passwordEncoder = auth.inMemoryAuthentication().passwordEncoder(bCryptPasswordEncoder);

        //使用JDBC进行身份认证
        JdbcUserDetailsManagerConfigurer<AuthenticationManagerBuilder> jdbcUserDetailsManagerConfigurer = auth.jdbcAuthentication().passwordEncoder(bCryptPasswordEncoder);
        jdbcUserDetailsManagerConfigurer.dataSource(dataSource);
        jdbcUserDetailsManagerConfigurer.usersByUsernameQuery("select username,password,valid From t_customer WHERE username=?");
        jdbcUserDetailsManagerConfigurer.authoritiesByUsernameQuery("select c.username,a.authority FROM t_customer c,t_authority a,t_customer_authority ta WHERE c.id=ta.customer_id AND a.id=ta.authority_id AND c.username = ?");
    

    }
}
5.效果测试

然后根据数据库中的用户名和密码(123456)进行验证就可以

UserDetailsService(身份详情服务)

对于用户流量较大的项目来说,频繁的使用IDBC进行数据库查询认证不仅麻烦,而且会降低网站登录访问速度。对于一个完善的项目来说,如果某些业务已经实现了用户信息查询的服务,就没必要使用JDBC 进行身份认证了。

下面假设当前项目中已经有用户信息查询的业务方法,这里,在已有的用户信息服务的基础上选择使用 UserDetailsService 进行自定义用户身份认证。

1.UserDetailsServiceImpl 实现 UserDetailsService接口
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private CustomerService customerService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //获取用户信息
        Customer customer = customerService.getCustomer(username);
        //获取权限
        List<Authority> customerAuthority = customerService.getCustomerAuthority(username);
        // 对用户权限进行封装
        List<SimpleGrantedAuthority> list = customerAuthority.stream().map(authority -> new SimpleGrantedAuthority(authority.getAuthority())).collect(Collectors.toList());

        // 返回封装的UserDetails用户详情类
// 返回封装的UserDetails用户详情类
        if(customer!=null){
            UserDetails userDetails= new User(customer.getUsername(),customer.getPassword(),list);
            return userDetails;
        } else {
            // 如果查询的用户不存在(用户名不存在),必须抛出此异常
            throw new UsernameNotFoundException("当前用户不存在!");
        }
    }
}
2.使用UserDetailsService进行身份认证
@EnableWebSecurity  //开启MVC Security安全支持
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Qualifier("dataSource")
    @Autowired
    private DataSource dataSource;

    @Autowired
    private UserDetailsServiceImpl userDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //设置密码编码器
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        InMemoryUserDetailsManagerConfigurer<AuthenticationManagerBuilder> passwordEncoder = auth.inMemoryAuthentication().passwordEncoder(bCryptPasswordEncoder);

        //使用UserDetailsService进行身份认证
        auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder);
    }
}
3.实体类都要实现序列化

Repository 层

public interface AuthorityRepository extends JpaRepository<Authority,Integer> {
    @Query(value = "select a.* from t_customer c,t_authority a,t_customer_authority ca where ca.customer_id=c.id and ca.authority_id=a.id and c.username =?1",nativeQuery = true)
    public List<Authority> findAuthoritiesByUsername(String username);
}
public interface CustomerRepository extends JpaRepository<Customer,Integer> {
    Customer findByUsername(String username);
}
4.效果测试 

;