导入所需的依赖 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
< project xmlns = " http://maven.apache.org/POM/4.0.0" xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance"
xsi: schemaLocation= " http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" >
< modelVersion> 4.0.0</ modelVersion>
< parent>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-parent</ artifactId>
< version> 2.3.7.RELEASE</ version>
< relativePath/>
</ parent>
< groupId> com.pan</ groupId>
< artifactId> springboot_SpringSecurity</ artifactId>
< version> 0.0.1-SNAPSHOT</ version>
< name> springboot_SpringSecurity</ name>
< description> springboot_SpringSecurity</ description>
< properties>
< java.version> 1.8</ java.version>
</ properties>
< dependencies>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-security</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-web</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-test</ artifactId>
< scope> test</ scope>
</ dependency>
< dependency>
< groupId> com.baomidou</ groupId>
< artifactId> mybatis-plus-boot-starter</ artifactId>
< version> 3.3.2</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
</ dependency>
< dependency>
< groupId> org.projectlombok</ groupId>
< artifactId> lombok</ artifactId>
</ dependency>
</ dependencies>
< build>
< plugins>
< plugin>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-maven-plugin</ artifactId>
</ plugin>
</ plugins>
</ build>
</ project>
1、连接数据库、
server :
port : 8011
spring :
datasource :
username : root
password : root
url : jdbc: mysql: //localhost: 3306/db01? useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
2、创建一个测试数据的表
CREATE TABLE ` admin_user`
(
` id` int NOT NULL AUTO_INCREMENT ,
` username` varchar ( 32 ) NOT NULL ,
` password` varchar ( 32 ) DEFAULT NULL ,
PRIMARY KEY ( ` id` )
) ENGINE = InnoDB AUTO_INCREMENT = 5 DEFAULT CHARSET = utf8mb3;
3、编写相关的实体类
@Data
public class AdminUser {
private Integer id;
private String username;
private String password;
}
4、编写mapper接口继承BaseMapper
@Mapper
@Repository
public interface UserMapper extends BaseMapper < AdminUser > {
}
5、编写配置类
@Configuration
public class SecurityConfigTest extends WebSecurityConfigurerAdapter {
@Resource
private UserDetailsService userDetailsService;
@Bean
PasswordEncoder passwordEncoder ( ) {
return new BCryptPasswordEncoder ( ) ;
}
@Override
protected void configure ( AuthenticationManagerBuilder auth) throws Exception {
auth. userDetailsService ( userDetailsService) . passwordEncoder ( passwordEncoder ( ) ) ;
}
}
6、编写业务层的方法
@Service ( "userDetailsService" )
public class MyUserService implements UserDetailsService {
@Resource
private UserMapper userMapper;
@Override
public UserDetails loadUserByUsername ( String userName) throws UsernameNotFoundException {
QueryWrapper < AdminUser > queryWrapper = new QueryWrapper < > ( ) ;
queryWrapper. eq ( "userName" , userName) ;
AdminUser user = userMapper. selectOne ( queryWrapper) ;
if ( user == null ) {
throw new UsernameNotFoundException ( "该用户不存在" ) ;
}
List < GrantedAuthority > admin = AuthorityUtils . commaSeparatedStringToAuthorityList ( "ADMIN" ) ;
return new User ( user. getUsername ( ) , new BCryptPasswordEncoder ( ) . encode ( user. getPassword ( ) ) , admin) ;
}
}
7、编写控制器
@RestController
@RequestMapping ( "test" )
public class TestController {
@RequestMapping ( "hello" )
public String helloSecurity ( ) {
return "hello security" ;
}
}
8、访问控制器测试即可