最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springsecurity6配置自定義路徑身份認(rèn)證的實(shí)現(xiàn)

 更新時(shí)間:2025年03月14日 11:40:21   作者:qq_43746935  
本文主要介紹了springsecurity6配置自定義路徑身份認(rèn)證的實(shí)現(xiàn),通過(guò)使用自定義的AuthorizationManager和MyService,可以實(shí)現(xiàn)更靈活的訪問(wè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)文章

最新評(píng)論

宜昌市| 吉木萨尔县| 上饶县| 桦甸市| 石楼县| 舒兰市| 筠连县| 科尔| 广德县| 信宜市| 南平市| 阿克| 铜川市| 巴彦淖尔市| 建水县| 五峰| 图木舒克市| 德阳市| 棋牌| 普安县| 吴江市| 吕梁市| 临清市| 新邵县| 永靖县| 金山区| 敦煌市| 房产| 正蓝旗| 茂名市| 霍邱县| 伽师县| 山东省| 清原| 泾阳县| 河间市| 长沙市| 渭源县| 金乡县| 石狮市| 穆棱市|