diff --git a/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/cyou/chenx221/handler/CustomAuthenticationFailureHandler.class b/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/cyou/chenx221/handler/CustomAuthenticationFailureHandler.class new file mode 100644 index 0000000..c72671d Binary files /dev/null and b/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/cyou/chenx221/handler/CustomAuthenticationFailureHandler.class differ diff --git a/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/cyou/chenx221/service/CustomUserDetailsService.class b/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/cyou/chenx221/service/CustomUserDetailsService.class new file mode 100644 index 0000000..95204d6 Binary files /dev/null and b/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/cyou/chenx221/service/CustomUserDetailsService.class differ diff --git a/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/spring-security.xml b/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/spring-security.xml new file mode 100644 index 0000000..8ba24f6 --- /dev/null +++ b/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/spring-security.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/project2/src/main/java/cyou/chenx221/handler/CustomAuthenticationFailureHandler.java b/project2/src/main/java/cyou/chenx221/handler/CustomAuthenticationFailureHandler.java new file mode 100644 index 0000000..184bb97 --- /dev/null +++ b/project2/src/main/java/cyou/chenx221/handler/CustomAuthenticationFailureHandler.java @@ -0,0 +1,21 @@ +package cyou.chenx221.handler; + +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.web.authentication.AuthenticationFailureHandler; +import org.springframework.stereotype.Component; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +@Component +public class CustomAuthenticationFailureHandler implements AuthenticationFailureHandler { + + @Override + public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { + // 在验证失败时执行的逻辑 + request.getSession().setAttribute("errorMessage", "用户名或密码不正确,请重新输入。"); + System.out.println("到过这里"); + response.sendRedirect("/login"); // 重定向到登录页面 + } +} diff --git a/project2/src/main/java/cyou/chenx221/service/CustomUserDetailsService.java b/project2/src/main/java/cyou/chenx221/service/CustomUserDetailsService.java new file mode 100644 index 0000000..c8ab566 --- /dev/null +++ b/project2/src/main/java/cyou/chenx221/service/CustomUserDetailsService.java @@ -0,0 +1,35 @@ +package cyou.chenx221.service; + +import cyou.chenx221.mapper.UserMapper; +import cyou.chenx221.pojo.User; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.stereotype.Service; + +@Service +public class CustomUserDetailsService implements UserDetailsService { + + private final UserMapper userMapper; + + @Autowired + public CustomUserDetailsService(UserMapper userMapper) { + this.userMapper = userMapper; + } + + @Override + public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { + User user = userMapper.getUserByUsername(username); + if (user == null) { + throw new UsernameNotFoundException("X User not found with username: " + username); + } + System.out.println(user.getId()+" "+user.getUsername()+" "+user.getPassword()+" "+user.getRoles()); + // 创建并返回自定义的UserDetails对象 + return org.springframework.security.core.userdetails.User.builder() + .username(user.getUsername()) + .password(user.getPassword()) + .roles(user.getRoles()) + .build(); + } +} diff --git a/project2/src/main/resources/spring-security.xml b/project2/src/main/resources/spring-security.xml new file mode 100644 index 0000000..8ba24f6 --- /dev/null +++ b/project2/src/main/resources/spring-security.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file