diff --git a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/WebSecurityConfigurer.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/WebSecurityConfigurer.java index 74fb448a9..bf218dd7e 100644 --- a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/WebSecurityConfigurer.java +++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/WebSecurityConfigurer.java @@ -19,6 +19,7 @@ package com.pig4cloud.pigx.auth.config; +import com.pig4cloud.pigx.common.security.handler.FormAuthenticationFailureHandler; import com.pig4cloud.pigx.common.security.handler.MobileLoginSuccessHandler; import com.pig4cloud.pigx.common.security.mobile.MobileSecurityConfigurer; import lombok.SneakyThrows; @@ -32,6 +33,7 @@ import org.springframework.security.config.annotation.web.builders.WebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.factory.PasswordEncoderFactories; import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.security.web.authentication.AuthenticationFailureHandler; import org.springframework.security.web.authentication.AuthenticationSuccessHandler; /** @@ -47,18 +49,19 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { @SneakyThrows protected void configure(HttpSecurity http) { http - .formLogin() - .loginPage("/token/login") - .loginProcessingUrl("/token/form") - .and() - .authorizeRequests() - .antMatchers( - "/token/**", - "/actuator/**", - "/mobile/**").permitAll() - .anyRequest().authenticated() - .and().csrf().disable() - .apply(mobileSecurityConfigurer()); + .formLogin() + .loginPage("/token/login") + .loginProcessingUrl("/token/form") + .failureHandler(authenticationFailureHandler()) + .and() + .authorizeRequests() + .antMatchers( + "/token/**", + "/actuator/**", + "/mobile/**").permitAll() + .anyRequest().authenticated() + .and().csrf().disable() + .apply(mobileSecurityConfigurer()); } /** @@ -78,6 +81,11 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { return super.authenticationManagerBean(); } + @Bean + public AuthenticationFailureHandler authenticationFailureHandler() { + return new FormAuthenticationFailureHandler(); + } + @Bean public AuthenticationSuccessHandler mobileLoginSuccessHandler() { return new MobileLoginSuccessHandler(); diff --git a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/endpoint/PigxTokenEndpoint.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/endpoint/PigxTokenEndpoint.java index 4de06ef49..b7732e0fe 100644 --- a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/endpoint/PigxTokenEndpoint.java +++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/endpoint/PigxTokenEndpoint.java @@ -74,11 +74,13 @@ public class PigxTokenEndpoint { * 认证页面 * * @param modelAndView + * @param error 表单登录失败处理回调的错误信息 * @return ModelAndView */ @GetMapping("/login") - public ModelAndView require(ModelAndView modelAndView) { + public ModelAndView require(ModelAndView modelAndView, @RequestParam(required = false) String error) { modelAndView.setViewName("ftl/login"); + modelAndView.addObject("error", error); return modelAndView; } diff --git a/pigx-auth/src/main/resources/logback-spring.xml b/pigx-auth/src/main/resources/logback-spring.xml index 00fc55116..998f18b5c 100644 --- a/pigx-auth/src/main/resources/logback-spring.xml +++ b/pigx-auth/src/main/resources/logback-spring.xml @@ -80,7 +80,7 @@ - + diff --git a/pigx-auth/src/main/resources/templates/ftl/login.ftl b/pigx-auth/src/main/resources/templates/ftl/login.ftl index 3539f841e..60e8d90f5 100755 --- a/pigx-auth/src/main/resources/templates/ftl/login.ftl +++ b/pigx-auth/src/main/resources/templates/ftl/login.ftl @@ -1,6 +1,6 @@ - + @@ -12,20 +12,23 @@ - + - -
- - - + <#if error??> + ${error} + + +
+ + diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/handler/FormAuthenticationFailureHandler.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/handler/FormAuthenticationFailureHandler.java new file mode 100644 index 000000000..ea54c444f --- /dev/null +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/handler/FormAuthenticationFailureHandler.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.common.security.handler; + +import cn.hutool.http.HttpUtil; +import com.pig4cloud.pigx.common.core.util.WebUtils; +import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.web.authentication.AuthenticationFailureHandler; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.nio.charset.Charset; + +/** + * @author lengleng + * @date 2019-08-20 + *

+ * 表单登录失败处理逻辑 + */ +@Slf4j +public class FormAuthenticationFailureHandler implements AuthenticationFailureHandler { + /** + * Called when an authentication attempt fails. + * + * @param request the request during which the authentication attempt occurred. + * @param response the response. + * @param exception the exception which was thrown to reject the authentication + */ + @Override + @SneakyThrows + public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) { + log.debug("表单登录失败:{}", exception.getLocalizedMessage()); + WebUtils.getResponse().sendRedirect(String.format("/token/login?error=%s" + , HttpUtil.encodeParams(exception.getMessage(), Charset.defaultCharset()))); + } +} \ No newline at end of file