自定义数据库数据权限验证
基于springboot的web项目
引入必要的依赖包
1 2 3 4 5 6 7 8 9 10 11 12
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
|
添加数据库的连接配置信息
1 2 3 4 5 6
| spring: datasource: url: jdbc:mysql://**/db?useUnicode=true&characterEncoding=utf-8&useSSL=false username: name password: password driver-class-name: com.mysql.jdbc.Driver
|
创建表结构语句:
1 2 3 4 5 6 7 8 9
| CREATE TABLE `db`.`base_user` ( `id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '名称', `addtime` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '添加时间', `password` varchar(32) CHARACTER SET gbk COLLATE gbk_chinese_ci NULL DEFAULT NULL COMMENT '密码', PRIMARY KEY (`id`) USING BTREE ) ;
INSERT INTO `db`.`base_user`(`id`, `name`,`addtime`, `password`) VALUES ('1', 'admin', 'buzhidao');
|
添加自定义的UserDetailsService
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException;
@Configuration public class CustomUserDetailsService implements UserDetailsService {
@Autowired private JdbcTemplate jdbcTemplate;
@Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { Map<String, Object> user = jdbcTemplate.queryForMap("select id,name,password from base_user where name='" + username + "'"); if (user.size() > 0) { return User.withUsername(username).password(user.get("password").toString()).roles("test").build(); } return null; } }
|
添加权限验证类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity public class WbUserSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private CustomUserDetailsService customUserDetailsService;
@Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().anyRequest().authenticated() .and().formLogin().loginPage("/login").permitAll() .and().rememberMe().tokenValiditySeconds(2419200).key("wbuserkey"); }
@Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(customUserDetailsService); }
}
|
登陆页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title>login</title> </head> <body> <form action="/login" method="POST"> <input name="username"/> <input name="password" type="password"/> <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/> <input type="checkbox" name="remember-me"/><label>记住我</label> <button type="submit">登陆</button>
</form> </body> </html>
|
注意: 添加权限类的时候注意auth.userDetailsService(customUserDetailsService);,customUserDetailsService要自动注入,并且要写在参数为AuthenticationManagerBuilder的重载方法中。