1.自定义登陆页面
- WebSecurityConfig如下配置
//重写该方法 : 自定义配置过滤规则
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin()//表单登录
.loginPage("/login.html")//指定自己的登陆页面
.loginProcessingUrl("/authentication/login")//配置登录接口路径,需要与页面form表单的action保持一致 告诉security处理该路径
.and()
.authorizeRequests()//配置权限
.antMatchers("/login.html").permitAll()//指定所有用户进行访问指定的url
.anyRequest()//任何请求
.authenticated()//都需要身份认证
.and()
.csrf().disable();//关闭跨域
}
- 在/resources/static目录下编写登陆页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登陆</title>
</head>
<body>
<h1>标准登陆</h1>
<form action="/authentication/login" method="POST">
用户名 :<input type="text" name="username"><br>
密码 :<input type="text" name="password"><br>
<input type="submit" value="登陆"><br>
</form>
</body>
</html>
- 注意 上面输入框的name 必须是 username 和password
- 因为在UsernamePasswordAuthenticationFilter过滤器里 默认用户名和密码如下
- security认证需要经过该过滤器接受参数
- 重启访问项目http://127.0.0.1:9747/product
自定义页面成功
2. 登陆成功和失败的处理方法
2.1 方法一
-
当我们登录成功的时候,是由AuthenticationSuccessHandler进行登录结果处理,默认跳转到defaultSuccessUrl配置的路径对应的资源页面
-
当我们登录失败的时候,是由AuthenticationfailureHandler进行登录结果处理,默认跳转到failureUrl配置的路径对应的资源页面
-
我们先编写接口实现接口跳转 目前不实现页面跳转 逻辑一样
@RestController
public class JumpPageController {
//登陆成功返回
@GetMapping("/success")
public String success(){
return "success";
}
//登陆失败返回
@GetMapping("/failure")
public String failure(){
return "failure";
}
}
- WebSecurityConfig如下配置
- 重新启动 访问
- 失败
- 成功
2.2 方法二 自定义handler
- 创建LoginFailureHandler类 失败后的处理器
@Component
public class LoginFailureHandler extends SimpleUrlAuthenticationFailureHandler {
private Logger logger = LoggerFactory.getLogger(getClass());
/**
*
* @param request
* @param response
* @param exception : 登陆失败的原因
*/
@Override
public void onAuthenticationFailure(HttpServletRequest request,
HttpServletResponse response,
AuthenticationException exception) throws IOException {
//exception.getMessage() : 返回登陆失败的原因
logger.error("登录错误 : {}",exception.getMessage());
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);//400状态码
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);//设置请求头 : application/json;charset=UTF-8
//创建输出对象
PrintWriter writer = response.getWriter();
if ("Bad credentials".equals(exception.getMessage())) {
writer.write("密码不正确");
}else {
writer.write(exception.getMessage());
}
writer.flush();
writer.close();
}
}
- 创建LoginSuccessHandler 登陆成功处理器
@Component
public class LoginSuccessHandler implements AuthenticationSuccessHandler {
private Logger logger = LoggerFactory.getLogger(getClass());
/**
*
* @param request
* @param response
* @param authentication 保存了我们登录后的用户信息
*/
@Override
public void onAuthenticationSuccess(HttpServletRequest request,
HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
//authentication.getPrincipal() : 获取登录后的用户信息
logger.info("登陆成功 : {}",authentication.getPrincipal());
response.setStatus(HttpServletResponse.SC_OK);//200状态码
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);//设置请求头为 application/json;charset=UTF-8
PrintWriter writer = response.getWriter();
writer.write("登录成功" + authentication.getPrincipal());
writer.flush();
writer.close();
}
}
- WebSecurityConfig里配置如下信息
- 启动项目访问
- 失败
- 成功