springboot集合springsecurity,mybatis (属性有 用户名,密码,权限),举例说明,要求每个步骤都有
时间: 2024-06-11 20:09:50 浏览: 210
1. 配置pom.xml文件,添加Spring Boot、Spring Security和MyBatis的依赖:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
```
2. 在application.properties文件中配置Spring Security的用户名、密码和权限:
```
spring.security.user.name=admin
spring.security.user.password=admin
spring.security.user.roles=ADMIN
```
3. 创建数据库表,包含用户信息和用户权限信息:
```
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT NULL,
`password` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
CREATE TABLE `user_role` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`role` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
```
4. 创建User实体类和UserRole实体类,用于映射数据库表:
```
public class User {
private Long id;
private String username;
private String password;
// 省略getter和setter方法
}
public class UserRole {
private Long id;
private Long userId;
private String role;
// 省略getter和setter方法
}
```
5. 创建UserMapper接口和UserRoleMapper接口,用于操作数据库表:
```
@Mapper
public interface UserMapper {
User findByUsername(String username);
}
@Mapper
public interface UserRoleMapper {
List<UserRole> findByUserId(Long userId);
}
```
6. 创建UserService类,用于实现UserDetailsService接口,用于加载用户信息:
```
@Service
public class UserService implements UserDetailsService {
@Autowired
private UserMapper userMapper;
@Autowired
private UserRoleMapper userRoleMapper;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userMapper.findByUsername(username);
if (user == null) {
throw new UsernameNotFoundException("用户不存在");
}
List<UserRole> userRoles = userRoleMapper.findByUserId(user.getId());
List<GrantedAuthority> authorities = new ArrayList<>();
for (UserRole userRole : userRoles) {
authorities.add(new SimpleGrantedAuthority(userRole.getRole()));
}
return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), authorities);
}
}
```
7. 创建WebSecurityConfig类,用于配置Spring Security:
```
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserService userService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userService).passwordEncoder(new BCryptPasswordEncoder());
}
}
```
8. 创建AdminController类,用于测试Spring Security:
```
@Controller
@RequestMapping("/admin")
public class AdminController {
@GetMapping("/test")
@ResponseBody
public String test() {
return "admin test";
}
}
```
9. 启动Spring Boot应用程序,访问http://localhost:8080/login,输入用户名和密码,即可访问http://localhost:8080/admin/test页面。
阅读全文