springsecurity整合springboot实现自定义登录页面及访问路径权限配置
springsecurity整合springboot实现自定义登录页面及访问路径权限配置
编写配置类继承WebSecurityConfigurerAdapter,重写void configure(HttpSecurity http)方法
package com.yl.config;
import com.yl.service.impl.MyUserDetailService;
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.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
/**
* spring security配置类
*
* @author Y-wee
*/
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// 登录表单配置
http.formLogin()
// 设置登录页面
.loginPage("/login.html")
/**
* 设置登录请求路径(自定义),登录页面的登录请求路径必须跟这里保持一致
* 该请求实现逻辑不需要我们自己写,我们只需要配置好路径即可,逻辑spring security会帮我们处理
*/
.loginProcessingUrl("/user/login")
// 设置登录成功之后跳转的路径,permitAll()表示任何角色都可以访问
.defaultSuccessUrl("/index.html").permitAll()
// 认证配置
.and().authorizeRequests()
// 设置路径不需要认证即可访问,注意登录请求路径配置在这里
.antMatchers("/", "/login/noLogin", "/user/login").permitAll()
/**
* 设置路径只有admin角色可以访问,与hasRole()方法类似,不同之处:
* hasRole()默认会给角色加上ROLE_前缀(可点进方法查看源码验证)
*/
.antMatchers("/login/admin").hasAuthority("admin")
// 设置路径admin和normal角色都可以访问
.antMatchers("/login/normal").hasAnyAuthority("admin", "normal")
/**
* 设置任何尚未匹配的URL只需要经过认证就可以访问
* 注意:anyRequest()只能配置在antMatchers()后面
* 自定义登录页面后如果没有anyRequest()的相关配置,则任何尚未匹配的url不经过认证(登录)也可以访问
*/
.anyRequest().authenticated()
// 设置任何尚未匹配的URL都将被拒绝访问
// .anyRequest().denyAll()
// 关闭csrf防护
.and().csrf().disable();
// 设置403(没有权限)跳转页面
http.exceptionHandling().accessDeniedPage("/403.html");
// 注销配置
http.logout()
// 设置注销url
.logoutUrl("/logout")
// 设置注销成功要跳转的url
.logoutSuccessUrl("/login.html");
}
}
中文文档解释:
中文文档地址:https://www.springcloud.cc/spring-security.html#jc-form
注意:登录表单的用户名和密码属性默认必须为username和password,如果要自定义则加上如下配置:
http.formLogin()
.usernameParameter("用户名参数名")
.passwordParameter("密码参数名");
基于注解实现访问路径角色权限控制
在配置类或者启动类开启注解功能
// 启用@Secured\@PreAuthorize\@PostAuthorize注解
@EnableGlobalMethodSecurity(securedEnabled = true,prePostEnabled = true)
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {}
在controller加注解进行角色权限控制
@GetMapping("/update1")
// 用户必须具有ROLE_update角色才能访问该路径,@Secured注解判断的角色必须以ROLE_前缀修饰
@Secured("ROLE_update")
public String update1(){
return "welcome to update";
}
@GetMapping("/update2")
@PreAuthorize("hasAnyAuthority('update')")
public String update2(){
return "welcome to update";
}
@GetMapping("/update3")
@PostAuthorize("hasAnyRole('update')")
public String update3(){
return "welcome to update";
}
@PostAuthorize和@Secured的作用应该一看就懂,不作过多解释,其区别跟配置类里面写的一样
具体解释可参考官网:https://docs.spring.io/spring-security/site/docs/5.3.4.RELEASE/reference/html5/#el-access
记得快乐
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!