| public class SecurityConfig extends WebSecurityConfigurerAdapter { |
| |
| @Autowired |
| private UserDetailsService userDetailsService; |
| |
| |
| @Override |
| protected void configure(AuthenticationManagerBuilder auth) throws Exception { |
| auth.userDetailsService(userDetailsService).passwordEncoder(password()); |
| } |
| |
| |
| @Bean |
| PasswordEncoder password() { |
| return new BCryptPasswordEncoder(); |
| } |
| |
| private final ObjectMapper objectMapper; |
| |
| private Filter restAuthenticationFilter() throws Exception { |
| RestAuthenticationFilter filter = new RestAuthenticationFilter(objectMapper); |
| filter.setAuthenticationSuccessHandler(getAuthenticationSuccessHandler()); |
| filter.setAuthenticationFailureHandler(getAuthenticationFailureHandler()); |
| filter.setAuthenticationManager(authenticationManager()); |
| |
| filter.setFilterProcessesUrl("/authorize/login"); |
| return filter; |
| } |
| |
| @Override |
| protected void configure(HttpSecurity http) throws Exception { |
| |
| http |
| |
| .csrf().disable() |
| |
| .cors().configurationSource(corsConfigurationSource()); |
| |
| http.logout().logoutUrl("/logout"); |
| |
| http.exceptionHandling().accessDeniedPage("/403.html"); |
| |
| http.authorizeRequests(req -> req |
| |
| .antMatchers("/test", "/file/**", "/**/swagger/**", |
| "/user/**", "/agencies/**", "/zoneSet/**", "/plateSet/**", "/precinctsSet/**", "/intentManagement/**", "/contract/**" |
| ).permitAll() |
| |
| |
| |
| |
| |
| .anyRequest().authenticated()) |
| .addFilterAt(restAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class) |
| .formLogin(form -> form.loginProcessingUrl("/login").permitAll() |
| |
| .successHandler(getAuthenticationSuccessHandler()) |
| |
| .failureHandler(getAuthenticationFailureHandler()) |
| .permitAll()) |
| .httpBasic(Customizer.withDefaults()) |
| .csrf(csrf -> csrf.disable()) |
| .logout(logout -> logout.logoutUrl("/logout") |
| |
| .logoutSuccessHandler(getLogoutSuccessHandler())); |
| } |
| |
| |
| private AuthenticationSuccessHandler getAuthenticationSuccessHandler() { |
| return (req, res, auth) -> { |
| res.setStatus(HttpStatus.OK.value()); |
| res.getWriter().println(); |
| log.debug("认证成功!"); |
| |
| Map<Object, Object> map = new HashMap<>(); |
| map.put("code",200); |
| map.put("msg","login success"); |
| map.put("data","true"); |
| String jsonMap = new ObjectMapper().writeValueAsString(map); |
| res.setContentType("application/json;charset=UTF-8"); |
| res.getWriter().println(jsonMap); |
| }; |
| } |
| |
| |
| private AuthenticationFailureHandler getAuthenticationFailureHandler() { |
| return (req, res, exp) -> { |
| ObjectMapper objectMapper = new ObjectMapper(); |
| res.setStatus(HttpStatus.UNAUTHORIZED.value()); |
| res.setContentType(MediaType.APPLICATION_JSON_VALUE); |
| res.setCharacterEncoding("UTF-8"); |
| res.getWriter().println(objectMapper.writeValueAsString(exp.getMessage())); |
| log.debug("认证失败!"); |
| Map<Object, Object> map = new HashMap<>(); |
| map.put("code",201); |
| map.put("msg","login fail"); |
| map.put("data","false"); |
| String jsonMap = new ObjectMapper().writeValueAsString(map); |
| res.setContentType("application/json;charset=UTF-8"); |
| res.getWriter().println(jsonMap); |
| }; |
| } |
| |
| |
| private LogoutSuccessHandler getLogoutSuccessHandler() { |
| return (req, resp, auth) -> { |
| ObjectMapper objectMapper = new ObjectMapper(); |
| resp.setStatus(HttpStatus.OK.value()); |
| resp.setContentType(MediaType.APPLICATION_JSON_VALUE); |
| |
| |
| log.debug("退出登录成功!"); |
| Map<Object, Object> map = new HashMap<>(); |
| map.put("code",200); |
| map.put("msg","logout success"); |
| map.put("data","true"); |
| String jsonMap = new ObjectMapper().writeValueAsString(map); |
| resp.setContentType("application/json;charset=UTF-8"); |
| resp.getWriter().println(jsonMap); |
| }; |
| } |
| |
| |
| private CorsConfigurationSource corsConfigurationSource() { |
| UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); |
| CorsConfiguration corsConfiguration = new CorsConfiguration(); |
| corsConfiguration.addAllowedOrigin("*"); |
| corsConfiguration.addAllowedHeader("*"); |
| corsConfiguration.addAllowedMethod("*"); |
| source.registerCorsConfiguration("/**", corsConfiguration); |
| return source; |
| } |
| |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?