Spring Boot 整合 Security 权限控制 - 第005章 - 实现登录验证/提示功能

视频课程地址: Spring Boot 整合 Security 权限控制

  • 配置登录失败跳转地址, 修改WebSecurityConfig配置文件
@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable()
            // 允许直接访问/路径
            .authorizeRequests().antMatchers("/").permitAll()
            // 使其支持跨域
            .requestMatchers(CorsUtils::isPreFlightRequest).permitAll()
            // 其他路径需要授权访问
            .anyRequest().authenticated()
            // 指定登录页面
            .and().formLogin().loginPage("/user/login")
            // 指定登录失败跳转地址
            .failureUrl("/user/login?error").permitAll()
            // 登录成功后的默认路径
            .defaultSuccessUrl("/").permitAll()
            // 退出登录后的默认路径
            .and().logout().logoutSuccessUrl("/user/login").permitAll();
}
  • 修改登录页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"><head>
    <meta charset="UTF-8"/>
    <title>登录页面</title>
</head>
<body>

<form action="/user/login" method="post">
    <table>
        <tr>
            <p th:if="${param.error}" class="bg-danger">有错误,请重试</p>
        </tr>
        <tr>
            <td>用户名:</td>
            <td><input name="username" id="username" value=""/></td>
        </tr>
        <tr>
            <td>密码:</td>
            <td><input name="password" id="password" value=""/></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="submit"/></td>
        </tr>
    </table>
</form>

</body>
</html>

猜你喜欢

转载自blog.csdn.net/shrcheng/article/details/79724040