springsecurity6配置自定義路徑身份認(rèn)證的實(shí)現(xiàn)
Spring Security 6 作為最新版本,引入了許多新特性和改進(jìn),例如對(duì) Spring Framework 6 的支持、新的默認(rèn)密碼編碼器、更簡(jiǎn)潔的配置方式等。
springsecurity6配置自定義路徑身份認(rèn)證 .anyRequest().authenticated()替換成
.anyRequest().access(new CustomAuthorizationManager(myService))
CustomAuthorizationManager
package com.example.springscuritydemo.config;
import com.example.springscuritydemo.service.MyService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.security.authorization.AuthorizationDecision;
import org.springframework.security.authorization.AuthorizationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.access.intercept.RequestAuthorizationContext;
import java.util.function.Supplier;
public class CustomAuthorizationManager implements AuthorizationManager<RequestAuthorizationContext> {
private final MyService myService;
public CustomAuthorizationManager(MyService myService) {
this.myService = myService;
}
@Override
public AuthorizationDecision check(Supplier<Authentication> authentication, RequestAuthorizationContext context) {
HttpServletRequest request = context.getRequest();
Authentication auth = authentication.get();
if (auth == null) {
return new AuthorizationDecision(false);
}
return new AuthorizationDecision(myService.hasPermission(request, auth));
}
}
MyService
package com.example.springscuritydemo.service;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.security.core.Authentication;
public interface MyService {
boolean hasPermission(HttpServletRequest request, Authentication authentication);
}
MyServiceImpl
package com.example.springscuritydemo.service.impl;
import com.example.springscuritydemo.service.MyService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import java.util.Collection;
@Service
public class MyserviceImpl implements MyService {
@Override
public boolean hasPermission(HttpServletRequest request, Authentication authentication) {
Object obj = authentication.getPrincipal();
if (obj instanceof UserDetails) {
UserDetails userDetails = (UserDetails) obj;
Collection<? extends GrantedAuthority> authorities = userDetails.getAuthorities();
boolean contains = authorities.contains(new SimpleGrantedAuthority(request.getRequestURI()));
return contains;
}
return false;
}
}
package com.example.springscuritydemo.config;
import com.example.springscuritydemo.handle.MyAccessDeniedHandler;
import com.example.springscuritydemo.handle.MyAuthenticationSuccessHandler;
import com.example.springscuritydemo.service.MyService;
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.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.access.expression.WebExpressionAuthorizationManager;
@EnableWebSecurity
@Configuration
public class SecurityConfig{
@Autowired
private MyAccessDeniedHandler myAccessDeniedHandler;
// @Autowired
// private MyAuthenticationFailureHandler myAuthenticationFailureHandler;
private final MyService myService;
public SecurityConfig(MyService myService) {
this.myService = myService;
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http
.formLogin(formLogin -> formLogin.loginPage("/login.html")
.loginProcessingUrl("/login")
//.successForwardUrl("/toMain")
.successHandler(new MyAuthenticationSuccessHandler("/main.html"))
.failureUrl("/toError")
//.failureHandler(new MyAuthenticationFailureHandler("/error.html"))
)
.authorizeHttpRequests(auth -> auth.requestMatchers("/toError","/login.html","/error.html").permitAll()
//需要認(rèn)證才能訪問(wèn),是security的認(rèn)證。不是jwt的認(rèn)證登錄后訪問(wèn)
.requestMatchers("/js/**","/css/**","/img/**").permitAll()
.requestMatchers("main1.html")
.access(new WebExpressionAuthorizationManager("isAuthenticated() and hasIpAddress('192.168.10.6')"))
//其他路徑需要身份認(rèn)證
// .anyRequest().authenticated()
.anyRequest().access(new CustomAuthorizationManager(myService))
)
.csrf(httpSecurityCsrfConfigurer -> httpSecurityCsrfConfigurer.disable())
// 構(gòu)建并返回安全過(guò)濾鏈
.build();
}
}到此這篇關(guān)于springsecurity6配置自定義路徑身份認(rèn)證的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)springsecurity6自定義路徑身份認(rèn)證內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring-cloud-stream結(jié)合kafka使用詳解
這篇文章主要介紹了spring-cloud-stream結(jié)合kafka使用詳解,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
SpringBoot排查和解決JSON解析錯(cuò)誤(400 Bad Request)的方法
在開發(fā)Spring Boot RESTful API時(shí),客戶端與服務(wù)端的數(shù)據(jù)交互通常使用JSON格式,然而,JSON解析錯(cuò)誤(如400 Bad Request)是常見的開發(fā)問(wèn)題之一,本文將通過(guò)一個(gè)實(shí)際案例,詳細(xì)分析如何排查和解決JSON解析錯(cuò)誤,并總結(jié)最佳實(shí)踐,需要的朋友可以參考下2025-06-06
java中break和continue區(qū)別及使用場(chǎng)合分析
本文力圖通過(guò)實(shí)例加使用場(chǎng)合詳解來(lái)引導(dǎo)菜鳥重新認(rèn)識(shí)break和continue語(yǔ)句,需要的朋友可以參考下2014-01-01
Spring中的FactoryBean實(shí)現(xiàn)原理詳解
這篇文章主要介紹了Spring中的FactoryBean實(shí)現(xiàn)原理詳解,spring中有兩種類型的Bean,一種是普通的JavaBean,另一種就是工廠Bean(FactoryBean),這兩種Bean都受Spring的IoC容器管理,但它們之間卻有一些區(qū)別,需要的朋友可以參考下2024-02-02
Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之財(cái)務(wù)預(yù)算管理系統(tǒng)的實(shí)現(xiàn)
這是一個(gè)使用了java+SSM+Jsp+Mysql+Layui+Maven開發(fā)的財(cái)務(wù)預(yù)算管理系統(tǒng),是一個(gè)畢業(yè)設(shè)計(jì)的實(shí)戰(zhàn)練習(xí),具有財(cái)務(wù)預(yù)算管理該有的所有功能,感興趣的朋友快來(lái)看看吧2022-02-02
Spring Boot + Vue 前后端分離項(xiàng)目如何踢掉已登錄用戶
這篇文章主要介紹了Spring Boot + Vue 前后端分離項(xiàng)目如何踢掉已登錄用戶,需要的朋友可以參考下2020-05-05
Spring成為Java開發(fā)的標(biāo)準(zhǔn)以及SpringBoot如何徹底改變開發(fā)體驗(yàn)
本文深入剖析了Spring框架及其在Java企業(yè)級(jí)應(yīng)用開發(fā)中的地位,強(qiáng)調(diào)了Spring通過(guò)IoC容器、AOP和模塊化生態(tài)系統(tǒng)等核心特性,解決了一系列傳統(tǒng)JavaEE開發(fā)的痛點(diǎn),本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2025-12-12
springmvc path請(qǐng)求映射到bean 方法的流程
這篇文章主要介紹了springmvc path請(qǐng)求映射到bean 方法的流程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-07-07
Spring實(shí)戰(zhàn)之Bean的作用域request用法分析
這篇文章主要介紹了Spring實(shí)戰(zhàn)之Bean的作用域request用法,結(jié)合實(shí)例形式分析了spring中Bean的request作用域相關(guān)使用技巧與操作注意事項(xiàng),需要的朋友可以參考下2019-11-11

