diff --git a/src/main/java/com/al/_01springsecuritydemo01/config/SecurityConfig.java b/src/main/java/com/al/_01springsecuritydemo01/config/SecurityConfig.java index e811367..4026f71 100644 --- a/src/main/java/com/al/_01springsecuritydemo01/config/SecurityConfig.java +++ b/src/main/java/com/al/_01springsecuritydemo01/config/SecurityConfig.java @@ -1,5 +1,6 @@ package com.al._01springsecuritydemo01.config; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; @@ -7,10 +8,18 @@ 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.AuthenticationFailureHandler; +import org.springframework.security.web.authentication.AuthenticationSuccessHandler; @EnableWebSecurity // @Configuration 被包括在上面的注解了 public class SecurityConfig extends WebSecurityConfigurerAdapter { + @Autowired + private AuthenticationSuccessHandler successHandler; + + @Autowired + private AuthenticationFailureHandler failureHandler; + @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); @@ -21,8 +30,10 @@ protected void configure(HttpSecurity http) throws Exception { http.formLogin() .loginPage("/login.html") .loginProcessingUrl("/login") - .defaultSuccessUrl("/index") - .defaultSuccessUrl("/index") + // .defaultSuccessUrl("/index") + // .defaultSuccessUrl("/index") + .successHandler(successHandler) + .failureHandler(failureHandler) .and() .authorizeRequests() diff --git a/src/main/java/com/al/_01springsecuritydemo01/handler/MyAuthenticationFailureHandler.java b/src/main/java/com/al/_01springsecuritydemo01/handler/MyAuthenticationFailureHandler.java new file mode 100644 index 0000000..b5c3124 --- /dev/null +++ b/src/main/java/com/al/_01springsecuritydemo01/handler/MyAuthenticationFailureHandler.java @@ -0,0 +1,25 @@ +package com.al._01springsecuritydemo01.handler; + +import java.io.IOException; +import java.io.PrintWriter; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.web.authentication.AuthenticationFailureHandler; +import org.springframework.stereotype.Component; + +@Component +public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler { + + @Override + public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { + response.setContentType("application/json; charset=UTF-8"); + PrintWriter writer = response.getWriter(); + writer.write("{\"code\":\"40001\",\"msg\":\"登录失败\"}"); + writer.flush(); + writer.close(); + } +} diff --git a/src/main/java/com/al/_01springsecuritydemo01/handler/MyAuthenticationSuccessHandler.java b/src/main/java/com/al/_01springsecuritydemo01/handler/MyAuthenticationSuccessHandler.java new file mode 100644 index 0000000..183c480 --- /dev/null +++ b/src/main/java/com/al/_01springsecuritydemo01/handler/MyAuthenticationSuccessHandler.java @@ -0,0 +1,24 @@ +package com.al._01springsecuritydemo01.handler; + +import java.io.IOException; +import java.io.PrintWriter; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.security.core.Authentication; +import org.springframework.security.web.authentication.AuthenticationSuccessHandler; +import org.springframework.stereotype.Component; + +@Component +public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler { + @Override + public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { + response.setContentType("application/json; charset=UTF-8"); + PrintWriter writer = response.getWriter(); + writer.write("{\"code\":\"20001\",\"msg\":\"登录成功\"}"); + writer.flush(); + writer.close(); + } +} diff --git a/src/main/resources/static/login.html b/src/main/resources/static/login.html index db6f626..7d61080 100644 --- a/src/main/resources/static/login.html +++ b/src/main/resources/static/login.html @@ -3,13 +3,33 @@ 登录 +


- +
+ \ No newline at end of file