关于SpringSecurity配置403权限访问页面的完整代码
作者:别团等shy哥发育 时间:2023-11-13 02:03:59
1、未配置之前
2、开始配置
2.1 新建一个unauth.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>没有访问的权限</h1>
</body>
</html>
2.2 在继承WebSecurityConfigurerAdapter的配置类中设置
关键代码:
//配置没有权限访问自定义跳转的页面
http.exceptionHandling()
.accessDeniedPage("/unauth.html");
配置类完整代码:
package com.atguigu.springsecuritydemo1.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@Configuration
public class SecurityConfigTest extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(password());
}
@Bean
PasswordEncoder password(){
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
//退出配置
http.logout().logoutUrl("/logout")
.logoutSuccessUrl("/test/hello")
.permitAll();
//配置没有权限访问自定义跳转的页面
http.exceptionHandling().accessDeniedPage("/unauth.html");
http.formLogin() //自定义自己编写的登陆页面
.loginPage("/login.html") //登录页面设置
.loginProcessingUrl("/user/login") //登录访问路径
.defaultSuccessUrl("/success.html").permitAll() //登录成功之后,跳转路径
.and().authorizeRequests()
//设置哪些路径可以直接访问,不需要认证
.antMatchers("/","/test/hello","/user/login").permitAll()
//当前登录的用户,只有具有admins权限才可以访问这个路径
//1、hasAuthority方法
//.antMatchers("/test/index").hasAuthority("admins")
//2、hasAnyAuthority方法
// .antMatchers("/test/index").hasAnyAuthority("admins,manager")
//3、hasRole方法 ROLE_sale
.antMatchers("/test/index").hasRole("sale")
//4、hasAnyRole方法
.anyRequest().authenticated()
.and().csrf().disable(); //关闭csrf防护
}
}
2.3 继承UserDetailsService接口的实现类
package com.atguigu.springsecuritydemo1.service;
import com.atguigu.springsecuritydemo1.entity.Users;
import com.atguigu.springsecuritydemo1.mapper.UsersMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
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;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("userDetailsService")
public class MyUserDetailService implements UserDetailsService {
@Autowired
private UsersMapper usersMapper;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//调用userMapper中的方法,根据用户名查询数据库
QueryWrapper<Users> wrapper=new QueryWrapper<>();//条件构造器
//where username=?
wrapper.eq("username",username);
Users users= usersMapper.selectOne(wrapper);
//判断
if(users==null){ //数据库没有用户名,认证失败
throw new UsernameNotFoundException("用户名不存在!");
}
List<GrantedAuthority> auths= AuthorityUtils.commaSeparatedStringToAuthorityList("admins,ROLE_sale");
//从查询数据库返回user对象,得到用户名和密码,返回
return new User(users.getUsername(),new BCryptPasswordEncoder().encode(users.getPassword()),auths);
}
}
3、测试
现在我故意将原先的sale改为sale1制造错误
启动项目并访问http://localhost:8111/test/index
输入lucy 123
成功实现
来源:https://blog.csdn.net/qq_43753724/article/details/118022533
标签:SpringSecurity,403,权限访问页面
0
投稿
猜你喜欢
本地jvm执行flink程序带web ui的操作
2022-09-03 20:49:00
C# 获取属性名的方法
2023-03-05 07:02:18
如何通过Android Stduio来编写一个完整的天气预报APP
2023-10-11 17:45:01
如何从eureka获取服务的ip和端口号进行Http的调用
2021-07-05 05:28:13
Java反射根据不同方法名动态调用不同的方法(实例)
2022-05-08 00:51:47
C#动态编译并执行字符串样例
2022-02-10 22:26:53
分析Java中的类加载问题
2023-09-03 19:37:04
Java多线程编程综合案例详解
2023-12-09 18:13:25
Java中Optional的正确用法与争议点详解
2023-11-12 03:50:21
浅谈JavaWeb中的web.xml配置部署描述符文件
2023-11-12 00:14:13
深入理解Spring AOP
2023-02-09 15:14:40
JSON 与对象、集合之间的转换的示例
2021-12-04 20:08:58
SpringBoot分离打Jar包的两种配置方式
2023-01-30 09:06:59
jenkins安装及其配置笔记
2022-10-03 11:01:19
JavaWeb中的常用的请求传参注解说明
2023-06-19 03:12:06
Spring Boot应用监控的实战教程
2022-03-02 18:17:09
C# 图片格式转换的实例代码
2023-03-11 13:32:46
Java实现动态模拟时钟
2022-07-25 17:35:25
c#保存窗口位置大小操作类(序列化和文件读写功能)
2023-07-15 18:51:06
java实现数字转换人民币中文大写工具
2023-08-16 08:48:41