zhuo
2025-04-23 234b0ac195934b34c06045b2d2ef0f10e239dd8e
ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java
@@ -1,10 +1,13 @@
package com.ruoyi.framework.config;
import com.ruoyi.framework.web.ssoAuth.SsoCodeAuthenticationProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
@@ -21,9 +24,12 @@
import com.ruoyi.framework.security.handle.AuthenticationEntryPointImpl;
import com.ruoyi.framework.security.handle.LogoutSuccessHandlerImpl;
import java.util.ArrayList;
import java.util.List;
/**
 * spring security配置
 *
 *
 * @author ruoyi
 */
@EnableMethodSecurity(prePostEnabled = true, securedEnabled = true)
@@ -34,8 +40,16 @@
     * 自定义用户认证逻辑
     */
    @Autowired
    @Qualifier("UserDetailsServiceImpl")
    private UserDetailsService userDetailsService;
    /**
     * sso自定义校验
     */
    @Autowired
    @Qualifier("UserDetailsByOpenIdServiceImpl")
    private UserDetailsService userDetailsServiceBySSO;
    /**
     * 认证失败处理类
     */
@@ -53,7 +67,7 @@
     */
    @Autowired
    private JwtAuthenticationTokenFilter authenticationTokenFilter;
    /**
     * 跨域过滤器
     */
@@ -75,7 +89,16 @@
        DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
        daoAuthenticationProvider.setUserDetailsService(userDetailsService);
        daoAuthenticationProvider.setPasswordEncoder(bCryptPasswordEncoder());
        return new ProviderManager(daoAuthenticationProvider);
        // sso登陆鉴权 Provider
        SsoCodeAuthenticationProvider ssoCodeAuthenticationProvider = new SsoCodeAuthenticationProvider();
        ssoCodeAuthenticationProvider.setUserDetailsService(userDetailsServiceBySSO);
        List<AuthenticationProvider> providers = new ArrayList<>();
        providers.add(ssoCodeAuthenticationProvider);
        providers.add(daoAuthenticationProvider);
        return new ProviderManager(providers);
    }
    /**
@@ -111,9 +134,9 @@
            .authorizeHttpRequests((requests) -> {
                permitAllUrl.getUrls().forEach(url -> requests.antMatchers(url).permitAll());
                // 对于登录login 注册register 验证码captchaImage 允许匿名访问
                requests.antMatchers("/login", "/register", "/captchaImage").permitAll()
                requests.antMatchers("/login", "/register", "/captchaImage", "/getSsoAuthUrl", "/loginBySSO").permitAll()
                    // 静态资源,可匿名访问
                    .antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
                    .antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**", "/img/**", "/word/**").permitAll()
                    .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()
                    // 除上面外的所有请求全部需要鉴权认证
                    .anyRequest().authenticated();