JAVA全系列 教程
3762个小节阅读:7088.7k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
在后台管理API模块编写Spring Security配置类
xxxxxxxxxx
// security配置类
@Configuration
public class SecurityConfig {
// Spring Security配置
@Bean
protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// 自定义表单登录
http.formLogin(
form -> {
form.usernameParameter("username") // 用户名项
.passwordParameter("password") // 密码项
.loginProcessingUrl("/admin/login") // 登录提交路径
.successHandler(new MyLoginSuccessHandler()) // 登录成功处理器
.failureHandler(new MyLoginFailureHandler()); // 登录失败处理器
}
);
// 权限拦截配置
http.authorizeHttpRequests(
resp -> {
resp.requestMatchers("/login", "/admin/login").permitAll(); // 登录请求不需要认证
resp.anyRequest().authenticated();// 其余请求都需要认证
}
);
// 退出登录配置
http.logout(
logout -> {
logout.logoutUrl("/admin/logout") // 注销的路径
.logoutSuccessHandler(new MyLogoutSuccessHandler()) // 登出成功处理器
.clearAuthentication(true) // 清除认证数据
.invalidateHttpSession(true) // 清除session
}
);
// 异常处理
http.exceptionHandling(
exception -> {
exception.authenticationEntryPoint(new MyAuthenticationEntryPoint())// 未登录处理器
.accessDeniedHandler(new MyAccessDeniedHandler()); // 权限不足处理器
}
);
// 跨域访问
http.cors();
// 关闭csrf防护
http.csrf(csrf ->{
csrf.disable();
});
return http.build();
}
// 加密工具
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
}