Spring Security(二)设置登陆页权限

改变登陆页面

下面这个是Security的初始化页面,这个页面肯定是不行的,设计太过简单,这样我们可根据自己的需要将登陆页面进行修改
在这里插入图片描述
下面有一个简单登陆页面的代码,今天拿这个进行测试

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>登陆</title>
    <style type="text/css">
        #box{
            margin: 50px auto;
            width: 30%;
            height: 360px;
            background-color: #fff;
            text-align: center;
            border-radius: 15px;
            border: 2px #fff solid;
            box-shadow: 10px 10px 5px #000000;
        }
        .title {
            line-height: 58px;
            margin-top: 20px;
            font-size: 36px;
            color: #000;
            height: 58px;
        }
        #box:hover {
            border: 2px #fff solid;
        }
        .input {
            margin-top: 20px;
        }
        input {
            margin-top: 5px;
            outline-style: none;
            border: 1px solid #ccc;
            border-radius: 3px;
            padding: 13px 14px;
            width: 70%;
            font-size: 14px;
            font-weight: 700;
            font-family: "Microsoft soft";
        }
        button {
            margin-top: 20px;
            border: none;
            color: #000;
            padding: 15px 32px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            border-radius: 15px;
            background-color: #CCCCCC;
        }
    </style>
</head>
<body>
<form action="/login" method="post" th:action="@{/login}" th:method="post" class="form">
    <div>
        <div id="box">
            <div class="title">Login</div>
                <div>
            <input type="text" name="username" placeholder="用户名">
                    <br/>
            <input type="password" name="password" placeholder="密码">
                    <br/>
                    <button type="submit">登录</button>
                </div>
            </div>
        </div>
</form>
</body>
</html>

展示图
在这里插入图片描述
添加一个登陆的页面后在去Spring Security配置类添加一个权限开开放的配置就完成了,具体代码如下

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
 * @Author uncletj
 * @Date 2021/3/16
 * @Version SpringBoot 2.2.2
 * @Name Spring Sceurity配置类
 */
@EnableWebSecurity
@Configuration
public class SpringSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .formLogin().loginPage("/login").permitAll()
                .and()
                .authorizeRequests()
                .anyRequest()
                .authenticated()
        ;
    }
}

控制类直接返回

@RequestMapping("/login")
    public String login(){
        return "/login";
    }

登陆成功后跳转首页
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

藤井大叔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值