| | |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.http.HttpMethod; |
| | | import org.springframework.security.access.AccessDeniedException; |
| | | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; |
| | | import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| | | import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; |
| | | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| | | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| | | import org.springframework.security.config.http.SessionCreationPolicy; |
| | | import org.springframework.security.core.AuthenticationException; |
| | | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
| | | import org.springframework.security.web.AuthenticationEntryPoint; |
| | | import org.springframework.security.web.SecurityFilterChain; |
| | | import org.springframework.security.web.access.AccessDeniedHandler; |
| | | import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.ServletException; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import jakarta.servlet.ServletException; |
| | | import jakarta.servlet.http.HttpServletRequest; |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import jakarta.annotation.Resource; |
| | | import java.io.IOException; |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Configuration |
| | | @EnableWebSecurity |
| | | @EnableGlobalMethodSecurity(prePostEnabled = true) |
| | | public class SecurityConfig extends WebSecurityConfigurerAdapter { |
| | | @EnableMethodSecurity(prePostEnabled = true) |
| | | public class SecurityConfig { |
| | | |
| | | public static final String[] FILTER_PATH = new String[] { |
| | | "/demo/**", |
| | |
| | | "/system/info", |
| | | "/tenant/list", |
| | | "/email/code", |
| | | "/pda/**", |
| | | "/pda/login", |
| | | "/erp/**", |
| | | "/base/**", |
| | | "/order/**", |
| | |
| | | "/ws/**", |
| | | "/wcs/**", |
| | | "/monitor/**", |
| | | "/mcp/**" |
| | | "/mcp/**", |
| | | "/ai/mcp", |
| | | "/mes/**" |
| | | }; |
| | | |
| | | @Resource |
| | |
| | | @Resource |
| | | private JwtAuthenticationFilter jwtAuthenticationFilter; |
| | | |
| | | @Override |
| | | protected void configure(HttpSecurity http) throws Exception { |
| | | http.authorizeRequests() |
| | | .antMatchers(HttpMethod.OPTIONS, "/**") |
| | | .permitAll() |
| | | .antMatchers(HttpMethod.GET, "/file/**", "/captcha", "/") |
| | | .permitAll() |
| | | .antMatchers(FILTER_PATH) |
| | | .permitAll() |
| | | .anyRequest() |
| | | .authenticated() |
| | | .and() |
| | | .sessionManagement() |
| | | .sessionCreationPolicy(SessionCreationPolicy.STATELESS) |
| | | .and() |
| | | .csrf() |
| | | .disable() |
| | | .cors() |
| | | .and() |
| | | .logout() |
| | | .disable() |
| | | .headers() |
| | | .frameOptions() |
| | | .disable() |
| | | .and() |
| | | .exceptionHandling() |
| | | .accessDeniedHandler(jwtAccessDeniedHandler) |
| | | .authenticationEntryPoint(jwtAuthenticationEntryPoint) |
| | | .and() |
| | | .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class); |
| | | } |
| | | |
| | | @Bean |
| | | public BCryptPasswordEncoder bCryptPasswordEncoder() { |
| | | return new BCryptPasswordEncoder(); |
| | | public SecurityFilterChain securityFilterChain(org.springframework.security.config.annotation.web.builders.HttpSecurity http) |
| | | throws Exception { |
| | | http.authorizeHttpRequests(authorize -> authorize |
| | | .requestMatchers(HttpMethod.OPTIONS, "/**").permitAll() |
| | | .requestMatchers(HttpMethod.GET, "/file/**", "/captcha", "/").permitAll() |
| | | .requestMatchers(FILTER_PATH).permitAll() |
| | | .anyRequest().authenticated()) |
| | | .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) |
| | | .csrf(csrf -> csrf.disable()) |
| | | .cors(cors -> { |
| | | }) |
| | | .logout(logout -> logout.disable()) |
| | | .headers(headers -> headers.frameOptions(frameOptions -> frameOptions.disable())) |
| | | .exceptionHandling(exceptionHandling -> exceptionHandling |
| | | .accessDeniedHandler(jwtAccessDeniedHandler) |
| | | .authenticationEntryPoint(jwtAuthenticationEntryPoint)) |
| | | .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class); |
| | | |
| | | return http.build(); |
| | | } |
| | | |
| | | // 没有访问权限异常处理 |
| | |
| | | @Override |
| | | public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) |
| | | throws IOException, ServletException { |
| | | if (response.isCommitted()) { |
| | | return; |
| | | } |
| | | CommonUtil.responseError(response, Constants.UNAUTHORIZED_CODE, Constants.UNAUTHORIZED_MSG, e.getMessage()); |
| | | } |
| | | |
| | |
| | | @Override |
| | | public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) |
| | | throws IOException, ServletException { |
| | | if (response.isCommitted()) { |
| | | return; |
| | | } |
| | | CommonUtil.responseError(response, Constants.UNAUTHENTICATED_CODE, Constants.UNAUTHENTICATED_MSG, |
| | | e.getMessage()); |
| | | } |
| | |
| | | } |
| | | |
| | | } |
| | | |