关于SpringSecurity配置403权限访问页面的完整代码
作者:别团等shy哥发育 发布时间:2023-11-13 02:03:59
标签:SpringSecurity,403,权限访问页面
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


猜你喜欢
- Okhttp 处理了很多网络疑难杂症,比如从很多常用的连接问题中自动恢复。如果你服务器配置了多个IP地址,当一个IP地址连接失败后Okhtt
- 一般在android显示一个View都是通过Activity的setContentView设置的,但是还有一种方法,可以直接使用Window
- 鼠标事件的事件源往往与容器相关,当鼠标进入容器、离开容器,或者在容器中单击鼠标、拖动鼠标时都会发生鼠标事件。java语言为处理鼠标事件提供两
- 一、简介在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,
- 前言:如果让大家说出一款国内比较热门的社交软件,那无疑就是QQ和微信了,说到微信,无不例外的会想到微信公众号和小程序,所以现在它们已经是很多
- 通常同步意味着一个任务的某个处理过程会对多个线程在用串行化处理,而
- 本文实例为大家分享了RecyclerView实现点击条目删除的具体代码,供大家参考,具体内容如下MainActivity.javapubli
- 本文实例为大家分享了Android实现支付宝记账饼图,点击旋转到最下面,供大家参考,具体内容如下代码:package com.example
- 本文实例为大家分享了winform实现五子棋游戏的具体代码,供大家参考,具体内容如下利用数组,根据新旧数组值的不同,获取那个点是什么棋子;说
- 本文实例讲述了C#简单实现SNMP的方法。分享给大家供大家参考。具体如下:/**C# Network Programming by Rich
- 本文实例为大家分享了java实现数字转换人民币中文大写的具体代码,供大家参考,具体内容如下业务场景:1.在人事业务系统开发的报表打印文书时经
- 1.依赖maven依赖如下,需要说明的是,spring-boot-starter-data-redis里默认是使用lettuce作为redi
- 本文实例讲述了C#将指定目录所有文件名转换成小写的方法。分享给大家供大家参考。具体如下:using System;using System.
- 一、字符串:1、访问String中的字符:string本身可看作一个Char数组。string s = "hello world&
- java中javaBean与Bean的深入理解JavaBean 是Java中的一种特殊的类,可以将多个对象封装到一个对象(bean)中。特点
- MainActivity如下: package cn.testjavascript; import java.util.StringToke
- 1、前期准备需要在Manifest中添加相关权限<uses-permission android:name="android
- 前言本篇教程偏向实战,程序猿直接copy代码加入到自己的项目中做简单的修修改改便可使用,而对于springboot以及mybatis不在此进
- 本文介绍了springboot前后台数据交互的示例代码,分享给大家,具体如下:1.在路径中传递数据,比如对某个数据的id:123前台发送:格
- 本文实例为大家分享了ActionBar实现tab导航效果的具体代码,供大家参考,具体内容如下先来说一说基础知识:一、基本使用方法1.获取Ac