一篇文章帶你搞定 springsecurity基於資料庫的認證(springsecurity整合mybatis)
一、前期配置
1. 加入依賴
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> <version>5.1.27</version> </dependency>
這裡選定的 mysql-connector-java
連線版本是 5.1.27,對應的 application.properties
為:
spring.datasource.url=jdbc:mysql://localhost:3306/yolo spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.username=root spring.datasource.password=root
如果是 8.0以上版本則 application.properties
需要對於 spring.datasource.url
serverTimezone
spring.datasource.username=root spring.datasource.password=root spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.url=jdbc:mysql://localhost:3306/security?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
2. 資料庫指令碼
SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for role -- ---------------------------- DROP TABLE IF EXISTS `role`; CREATE TABLE `role` ( `id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(32) DEFAULT NULL,`nameZh` varchar(32) DEFAULT NULL,PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of role -- ---------------------------- INSERT INTO `role` VALUES ('1','dba','資料庫管理員'); INSERT INTO `role` VALUES ('2','admin','系統管理員'); INSERT INTO `role` VALUES ('3','user','使用者'); -- ---------------------------- -- Table structure for user -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT,`username` varchar(32) DEFAULT NULL,`password` varchar(255) DEFAULT NULL,`enabled` tinyint(1) DEFAULT NULL,`locked` tinyint(1) DEFAULT NULL,PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES ('1','root','$2a$10$RMuFXGQ5AtH4wOvkUqyvuecpqUSeoxZYqilXzbz50dceRsga.WYiq','1','0'); INSERT INTO `user` VALUES ('2','0'); INSERT INTO `user` VALUES ('3','sang','0'); -- ---------------------------- -- Table structure for user_role -- ---------------------------- DROP TABLE IF EXISTS `user_role`; CREATE TABLE `user_role` ( `id` int(11) NOT NULL AUTO_INCREMENT,`uid` int(11) DEFAULT NULL,`rid` int(11) DEFAULT NULL,PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of user_role -- ---------------------------- INSERT INTO `user_role` VALUES ('1','1'); INSERT INTO `user_role` VALUES ('2','2'); INSERT INTO `user_role` VALUES ('3','2','2'); INSERT INTO `user_role` VALUES ('4','3','3'); SET FOREIGN_KEY_CHECKS=1;
二、定義實體類
1. 定義 User
實體類 User 需要實現 UserDetails 介面,因為每個人都可以定義 User 物件,但是每個人定義的 User物件不一樣,這就造成每個人設定的屬性不一樣,當系統需要判定使用者的登入狀態時,因為使用者名稱和密碼的名稱設定的五花八門,造成無法確定呼叫哪個
所以這裡要求所有的實體類實現 UserDetails 介面,它就相當於一個規範定義了登入驗證時需要的屬性名稱,所以的實體類都要符合這個規範。
public class User implements UserDetails { private Integer id; private String username; private String password; private Boolean enabled; private Boolean locked; private List<Role> roles; public List<Role> getRoles() { return roles; } public void setRoles(List<Role> roles) { this.roles = roles; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @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 void setUsername(String username) { this.username = username; } @Override public Collection<? extends GrantedAuthority> getAuthorities() { List<SimpleGrantedAuthority> authorities = new ArrayList<>(); for (Role role : roles) { authorities.add(new SimpleGrantedAuthority("ROLE_"+role.getName())); } return authorities; } @Override public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public void setEnabled(Boolean enabled) { this.enabled = enabled; } public void setLocked(Boolean locked) { this.locked = locked; } }
(1) accountNonExpired、accountNonLocked、credentialsNonExpired、enabled 這四個屬性分別用來描述使用者的狀態,表示賬戶是否沒有過期、賬戶是否沒有被鎖定、密碼是否沒有過期、以及賬戶是否可用。
(2)roles 屬性表示使用者的角色,User 和 Role 是多對多關係,用一個 @ManyToMany 註解來描述。
(3)getAuthorities 方法返回使用者的角色資訊,我們在這個方法中把自己的 Role 稍微轉化一下即可。
(1)這個集合是返回使用者的所有角色,因為從資料庫得到是 roles,但是需要的是一個集合形式的 getAuthorities
,所以需要對其進行處理。
@Override public Collection<? extends GrantedAuthority> getAuthorities() { List<SimpleGrantedAuthority> authorities = new ArrayList<>(); for (Role role : roles) { authorities.add(new SimpleGrantedAuthority("ROLE_"+role.getName())); } return authorities; }
這裡注意springsecurity 角色的認證有一個要求,必須是以 ROLE_
開始的,否則會出現問題:
authorities.add(new SimpleGrantedAuthority("ROLE_"+role.getName()));
當然也可以在資料中新增使用者時,就讓使用者以 ROLE_
開始,這樣就不用二次添加了
(2)另外需要注意這裡的 isAccountNonLocked()
,賬戶是否沒有被鎖定,資料庫中儲存的是:
所以這裡是對 locked 取的反
@Override public boolean isAccountNonLocked() { return !locked; }
2. 定義 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; } }
三、定義 Service
@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.getUserRolesById(user.getId())); return user; } }
我們自己定義的 UserService
需要實現 UserDetailsService
介面,實現該介面,就要實現介面中的方法,也就是 loadUserByUsername
,這個方法的引數就是使用者在登入的時候傳入的使用者名稱,根據使用者名稱去查詢使用者資訊(查出來之後,系統會自動進行密碼比對)。
四、定義 Mapper
1. UserMapper
@Mapper public interface UserMapper { User loadUserByUsername(String username); List<Role> getUserRolesById(Integer id); }
2. UserMapper.xml
<?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.javaboy.securitydb.mapper.UserMapper"> <select id="loadUserByUsername" resultType="org.yolo.securitymybatis.bean.User"> select * from user where username=#{username} </select> <select id="getUserRolesById" resultType="org.yolo.securitymybatis.bean.Role"> select * from role where id in (select rid from user_role where uid=#{id}) </select> </mapper>
Mapper 的位置:
所以需要為其新增資源路徑:資源配置路徑新增到 pom 檔案中
<resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources>
五、定義 SecurityConfig
@Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired UserService userService; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userService); } @Bean PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } @Bean RoleHierarchy roleHierarchy() { RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl(); String hierarchy = "ROLE_dba > ROLE_admin \n ROLE_admin > ROLE_user"; roleHierarchy.setHierarchy(hierarchy); return roleHierarchy; } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/dba/**").hasRole("dba") .antMatchers("/admin/**").hasRole("admin") .antMatchers("/user/**").hasRole("user") .anyRequest().authenticated() .and() .formLogin() .permitAll() .and() .csrf().disable(); } }
測試訪問:成功
到此這篇關於一篇文章帶你搞定 springsecurity基於資料庫的認證(springsecurity整合mybatis)的文章就介紹到這了,更多相關springsecurity整合mybatis內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!