侧边栏壁纸
博主头像
Devlive 开源社区博主等级

行动起来,活在当下

  • 累计撰写 122 篇文章
  • 累计创建 32 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

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

我是管理员哦
2024-02-02 / 0 评论 / 0 点赞 / 11 阅读 / 1704 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2024-02-02,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

配置登录失败跳转地址, 修改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>
0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区