12.31

package com.qhjc.config;

import com.qhjc.service.UserService;
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.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import jakarta.servlet.http.HttpServletResponse;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private UserService userService;

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userService).passwordEncoder(passwordEncoder());
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests()
            .antMatchers("/static/**", "/css/**", "/js/**", "/images/**").permitAll()
            .antMatchers("/login", "/doLogin").permitAll()
            .antMatchers("/client/**").hasRole("CLIENT")
            .antMatchers("/inspector/**").hasRole("INSPECTOR")
            .antMatchers("/manager/**").hasRole("MANAGER")
            .antMatchers("/director/**").hasRole("DIRECTOR")
            .anyRequest().authenticated()
            .and()
            .formLogin()
            .loginPage("/login")
            .loginProcessingUrl("/doLogin")
            .successHandler(customAuthenticationSuccessHandler())
            .failureUrl("/login?error=true")
            .permitAll()
            .and()
            .logout()
            .logoutUrl("/logout")
            .logoutSuccessUrl("/login?logout=true")
            .permitAll()
            .and()
            .csrf().disable()
            .exceptionHandling()
            .accessDeniedPage("/access-denied");
}

@Bean
public AuthenticationSuccessHandler customAuthenticationSuccessHandler() {
    return (request, response, authentication) -> {
        String role = authentication.getAuthorities().stream()
                .findFirst()
                .map(grantedAuthority -> grantedAuthority.getAuthority())
                .orElse("");

        String redirectUrl = "";
        switch (role) {
            case "ROLE_CLIENT":
                redirectUrl = "/client/dashboard";
                break;
            case "ROLE_INSPECTOR":
                redirectUrl = "/inspector/dashboard";
                break;
            case "ROLE_MANAGER":
                redirectUrl = "/manager/dashboard";
                break;
            case "ROLE_DIRECTOR":
                redirectUrl = "/director/dashboard";
                break;
            default:
                redirectUrl = "/login?error=role";
        }

        response.sendRedirect(redirectUrl);
    };
}

@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}

}

posted @ 2026-01-03 13:57  Cx330。  阅读(2)  评论(0)    收藏  举报