SpringBoot淺析安全管理之基於數據庫認證
1. 設計數據表
用戶表、角色表、用戶角色關聯表
建表語句
CREATE TABLE `role` ( `id` int(11) NOT NULL, `name` varchar(32) DEFAULT NULL, `nameZh` varchar(32) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `user` ( `id` int(11) NOT NULL, `username` varchar(32) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, `enabled` varchar(1) DEFAULT NULL, `locked` varchar(1) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `user_role` ( `id` int(11) NOT NULL, `uid` int(11) DEFAULT NULL, `rid` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
初始化數據
註意:角色名有一個默認的前綴 ROLE_
INSERT INTO `user`(`id`, `username`, `password`, `enabled`, `locked`) VALUES (1, 'root', '$2a$10$x6CBW1qnQqKPIxUSefZN7ebfTEiYNnfzzVjPJzlRhg5XyMzSWoO4e', '1', '0'); INSERT INTO `user`(`id`, `username`, `password`, `enabled`, `locked`) VALUES (2, 'admin', '$2a$10$x6CBW1qnQqKPIxUSefZN7ebfTEiYNnfzzVjPJzlRhg5XyMzSWoO4e', '1', '0'); INSERT INTO `user`(`id`, `username`, `password`, `enabled`, `locked`) VALUES (3, 'tangsan', '$2a$10$x6CBW1qnQqKPIxUSefZN7ebfTEiYNnfzzVjPJzlRhg5XyMzSWoO4e', '1', '0'); INSERT INTO `role`(`id`, `name`, `nameZh`) VALUES (1, 'ROLE_dba', '數據庫管理員'); INSERT INTO `role`(`id`, `name`, `nameZh`) VALUES (2, 'ROLE_admin', '系統管理員'); INSERT INTO `role`(`id`, `name`, `nameZh`) VALUES (3, 'ROLE_user', '普通用戶'); INSERT INTO `user_role`(`id`, `uid`, `rid`) VALUES (1, 1, 1); INSERT INTO `user_role`(`id`, `uid`, `rid`) VALUES (2, 1, 2); INSERT INTO `user_role`(`id`, `uid`, `rid`) VALUES (3, 2, 2); INSERT INTO `user_role`(`id`, `uid`, `rid`) VALUES (4, 3, 3);
2. 創建項目
MyBatis 靈活,JPA 便利,此處選擇前者,創建 Spring Boot Web 項目添加如下依賴:
<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.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency>
3. 配置數據庫
在 application.properties 中進行數據連接配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/jpa
4. 創建實體類
Role
public class Role { private Integer id; private String name; private String nameZh; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNameZh() { return nameZh; } public void setNameZh(String nameZh) { this.nameZh = nameZh; } }
User
public class User implements UserDetails { private Integer id; private String username; private String password; private Boolean enabled; private Boolean locked; private List<Role> roles; @Override public Collection<? extends GrantedAuthority> getAuthorities() { List<SimpleGrantedAuthority> authorities = new ArrayList<>(); for (Role role : roles) { authorities.add(new SimpleGrantedAuthority(role.getName())); } return authorities; } @Override public String getPassword() { return password; } @Override public String getUsername() { return username; } @Override public boolean isAccountNonExpired() { return true; } @Override public boolean isAccountNonLocked() { return !locked; } @Override public boolean isCredentialsNonExpired() { return true; } @Override public boolean isEnabled() { return enabled; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } public Boolean getEnabled() { return enabled; } public void setEnabled(Boolean enabled) { this.enabled = enabled; } public Boolean getLocked() { return locked; } public void setLocked(Boolean locked) { this.locked = locked; } public List<Role> getRoles() { return roles; } public void setRoles(List<Role> roles) { this.roles = roles; } }
代碼解釋:
用戶實體類需要實現 UserDetails 接口,並且實現該接口中的 7 個方法
| 方法名 | 解釋 |
| — | — |
| getAuthorities() | 獲取當前用戶對象所具有的角色信息 |
| getPassword() | 獲取當前用戶對象的密碼 |
| getUsername() | 獲取當前用戶對象的用戶名 |
| isAccountNonExpired() | 當前賬號是否未過期 |
| isAccountNonLocked() | 當前賬號是否未鎖定 |
| isCredentialsNonExpired() | 當前賬號密碼是否未過期 |
| isEnabled() | 當前賬號是否可用 |
用戶根據實際情況設置這 7 個方法的返回值。因為默認情況下不需要開發者自己進行密碼角色等信息的比對,開發者隻需要提供相關信息即可,例如 getPassword() 方法返回的密碼和用戶輸入的密碼不匹配,會自動拋出 BadCredentialsException 異常,isAccountNonExpired() 方法返回瞭 false ,會自動拋出 AccountExpiredException 異常,因此對開發者而言,隻需要按照數據庫中的數據在這裡返回相應的配置即可。此處因為數據庫中隻有 enabled 和 locked 字段,故帳號未過期和密碼未過期兩個方法都返回 true
getAuthorities() 用來獲取當前用戶所具有的角色信息,此處用戶所具有的角色存儲在 roles 屬性中,因此該方法直接遍歷 roles 屬性,然後構造 SimpleGrantedAuthority 集合並返回
5. 創建UserService
@Service public class UserService implements UserDetailsService { @Autowired UserMapper userMapper; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { User user = userMapper.loadUserByUsername(username); if (user == null) { throw new UsernameNotFoundException("賬戶不存在!"); } user.setRoles(userMapper.getUserRolesByUid(user.getId())); return user; } }
代碼解釋:
- 定義 UserService 實現 UserDetailsService 接口,並實現該接口中的 loadUserByUsername 方法,該方法的參數就是用戶登錄時輸入的用戶名,通過用戶名去數據庫中查找用戶,如果沒有查詢到用戶,就拋出一個賬號不存在的異常,如果查找到瞭用戶,就繼續查詢該用戶所具有的角色信息,並將獲取到的 user 對象返回,再由系統提供的 DaoAuthenticationProvider 類去比對密碼是否正確
- loadUserByUsername 方法將在用戶登錄時自動調用
涉及到的 UserMapper 和 UserMapper.xml 如下
@Mapper public interface UserMapper { User loadUserByUsername(String username); List<Role> getUserRolesByUid(Integer id); }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="org.sang.mapper.UserMapper"> <select id="loadUserByUsername" resultType="org.sang.model.User"> select * from user where username=#{username} </select> <select id="getUserRolesByUid" resultType="org.sang.model.Role"> select * from role r,user_role ur where r.id=ur.rid and ur.uid=#{id} </select> </mapper>
6. 配置Spring Security
@Configuration public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired UserService userService; @Bean PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userService); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/admin/**").hasRole("admin") .antMatchers("/db/**").hasRole("dba") .antMatchers("/user/**").hasRole("user") .anyRequest().authenticated() .and() .formLogin() .loginProcessingUrl("/login").permitAll() .and() .csrf().disable(); } }
此處的配置與上篇介紹的一致,唯一不同的是沒有配置內存用戶,而是將剛剛創建好的 UserService 配置到 AuthenticationManagerBuilder 中。
7.創建Controller
@RestController public class HelloController { @GetMapping("/admin/hello") public String admin() { return "hello admin"; } @GetMapping("/db/hello") public String dba() { return "hello dba"; } @GetMapping("/user/hello") public String user() { return "hello user"; } }
8. 測試
登錄 admin 用戶,訪問 /admin/hello,報瞭以下錯誤
Invalid bound statement (not found)
pom.xml 新增以下配置
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources> </build>
再次登錄訪問 /admin/hello,報瞭以下錯誤
Caused by: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: Illegal overloaded getter method with ambiguous type for property enabled in class class org.sang.model.User. This breaks the JavaBeans specification and can cause unpredictable results.
去掉 User 實體類中 enabled 屬性的 get set 方法,如下
public class User implements UserDetails { private Integer id; private String username; private String password; private Boolean enabled; private Boolean locked; private List<Role> roles; @Override public Collection<? extends GrantedAuthority> getAuthorities() { List<SimpleGrantedAuthority> authorities = new ArrayList<>(); for (Role role : roles) { authorities.add(new SimpleGrantedAuthority(role.getName())); } return authorities; } @Override public String getPassword() { return password; } @Override public String getUsername() { return username; } @Override public boolean isAccountNonExpired() { return true; } @Override public boolean isAccountNonLocked() { return !locked; } @Override public boolean isCredentialsNonExpired() { return true; } @Override public boolean isEnabled() { return enabled; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } public Boolean getLocked() { return locked; } public void setLocked(Boolean locked) { this.locked = locked; } public List<Role> getRoles() { return roles; } public void setRoles(List<Role> roles) { this.roles = roles; } }
再次登錄訪問 /admin/hello
訪問 /db/hello
訪問 /user/hello
到此這篇關於SpringBoot淺析安全管理之基於數據庫認證的文章就介紹到這瞭,更多相關SpringBoot數據庫認證內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- Spring Security動態權限的實現方法詳解
- Java實現用戶管理系統
- SpringBoot + JPA @ManyToMany的操作要點說明
- java實現航空用戶管理系統
- SpringBoot+SpringSecurity實現基於真實數據的授權認證