博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot-Mybatis-SpringSecurity-LayUI的简单用户权限管理
阅读量:2440 次
发布时间:2019-05-10

本文共 6742 字,大约阅读时间需要 22 分钟。

基于SpringBoot-Mybatis-SpringSecurity-LayUI的简单用户权限管理

项目实现效果

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

Pom文件:
4.0.0
org.springframework.boot
spring-boot-starter-parent
1.5.20.BUILD-SNAPSHOT
com.example
safe-demo
0.0.1-SNAPSHOT
safe-demo
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-security
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
org.thymeleaf.extras
thymeleaf-extras-springsecurity4
org.springframework.boot
spring-boot-starter-test
test
org.springframework.security
spring-security-test
test
org.springframework.boot
spring-boot-devtools
true
org.springframework
springloaded
1.2.6.RELEASE
ch.qos.logback
logback-core
1.1.8
ch.qos.logback
logback-classic
1.1.8
org.slf4j
slf4j-api
1.7.22
org.projectlombok
lombok
1.16.12
com.alibaba
druid
1.0.9
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
com.github.pagehelper
pagehelper
4.1.6
mysql
mysql-connector-java
5.1.39
com.google.code.gson
gson
2.8.0
org.springframework.boot
spring-boot-maven-plugin
spring-snapshots
Spring Snapshots
https://repo.spring.io/snapshot
true
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
spring-snapshots
Spring Snapshots
https://repo.spring.io/snapshot
true
spring-milestones
Spring Milestones
https://repo.spring.io/milestone

MySql表的设计:

在这里插入图片描述

没有设置外键,使用user_role来保存用户对应的角色,查询用户时需要使用关联查询

基本流程:

前端页面基本都是发送Ajax请求来获取Json数据,之后使用LayUI来进行渲染,请求页面的Url都在WebMvcConfig里面,Controller存放的都是返回Json数据格式的处理类

Spirng Security 核心配置类:

配置了访问权限,表单验证处理页面和Url以及验证成功以及失败的处理Handler,登出处理Handler,无权限访问时的处理Handler
public class BrowerSecurityConfig extends WebSecurityConfigurerAdapter {
private final static BCryptPasswordEncoder ENCODER = new BCryptPasswordEncoder(); @Bean public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder(); } @Bean public MyUserDetailService myUserDetailService(){
return new MyUserDetailService(); } @Autowired private UserLoginAuthenticationFailureHandler userLoginAuthenticationFailureHandler; @Autowired private UserLoginAuthenticationSuccessHandler userLoginAuthenticationSuccessHandler; @Autowired private UserLogoutSuccessHandler userLogoutSuccessHandler; @Autowired private UserAuthenticationAccessDeniedHandler userAuthenticationAccessDeniedHandler; @Override protected void configure(HttpSecurity http) throws Exception {
http .headers().frameOptions().sameOrigin()//设置弹出层 .and() .authorizeRequests() .antMatchers("/admin/**","/setUserAdmin","/setUser","/deleteUserById") .access("hasRole('ROLE_ADMIN')")//只有管理员才能访问 .antMatchers("/home","/static/**","/getAllUser","/register_page", "/register","/checkNameIsExistOrNot")//静态资源等不需要验证 .permitAll()//不需要身份认证 .anyRequest().authenticated()//其他路径必须验证身份 .and() .formLogin() .loginPage("/login_page")//登录页面 .loginProcessingUrl("/login") .usernameParameter("username") .passwordParameter("password") .failureHandler(userLoginAuthenticationFailureHandler)//验证失败处理 .successHandler(userLoginAuthenticationSuccessHandler)//验证成功处理 .permitAll()//登录页面不需要验证 .and() .logout() .logoutSuccessHandler(userLogoutSuccessHandler)//登出处理 .permitAll() .and() .csrf().disable() .exceptionHandling().accessDeniedHandler(userAuthenticationAccessDeniedHandler);//无权限时的处理 } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(myUserDetailService()).passwordEncoder(new PasswordEncoder() {
@Override public String encode(CharSequence charSequence) {
return ENCODER.encode(charSequence); } @Override public boolean matches(CharSequence charSequence, String s) {
if ( !ENCODER.matches(charSequence,s)){
// log.info("{}","密码对不上"); }else {
// log.info("{}","密码OK"); } return ENCODER.matches(charSequence,s); } }); }}

项目已经放在GitHub:

转载地址:http://fxcqb.baihongyu.com/

你可能感兴趣的文章
利用Apache+PHP3+MySQL建立数据库驱动的动态网站(转)
查看>>
C#中实现DataGrid双向排序(转)
查看>>
利用C语言小程序来解决大问题(转)
查看>>
简单方法在C#中取得汉字的拼音的首字母(转)
查看>>
.NET开发之中的17种正则表达式小结(转)
查看>>
编程秘籍:使C语言高效的四大绝招(转)
查看>>
配置XDM--一种Linux的图形登录界面(转)
查看>>
计算机加锁 把U盘变成打开电脑的钥匙(转)
查看>>
C#开发的两个基本编程原则的深入讨论(转)
查看>>
Fedora Core 4 基础教程 (上传完毕)(转)
查看>>
删除MSSQL危险存储过程的代码(转)
查看>>
红旗软件:树立国际的Linux品牌(转)
查看>>
Linux学习要点(转)
查看>>
影响mysqld安全的几个选项(转)
查看>>
最新版本Linux Flash 9 Beta开放发布(转)
查看>>
mysql事务处理(转)
查看>>
Fedora 显示设备配置工具介绍(转)
查看>>
FREEBSD 升级及优化全攻略(转)
查看>>
系统移民须知:Linux操作系统安装要点(转)
查看>>
在redhat系统中使用LVM(转)
查看>>