入门SpringSecurity——第一节、表单登录

SpringSecurity初体验

1.新建一个SpringBoot项目,添加Web和SpringSecurity依赖

在这里插入图片描述

2.写一个接口进行测试

在这里插入图片描述
当我们直接启动这个项目,访问这个

http://localhost:8080/hello

接口时,发现会重定向到一个login页面
在这里插入图片描述而默认的用户就是user,密码则是启动项目时,console栏生成的一串UUID
在这里插入图片描述
键入用户名和密码,则可以正常访问这个接口
在这里插入图片描述
由上述操作可以知道这些都是自动配置的,是一种约定,但是每次启动都会生成不一样的密码,较为麻烦,如何配置,让生成的密码不变,有两种方法

2.1.在application.yml中进行配置

在这里插入图片描述

2.2增加一个配置类在这里插入图片描述

1.首先自定义 SecurityConfig 继承自 WebSecurityConfigurerAdapter,重写里边的 configure 方法。
2. 首先我们提供了一个 PasswordEncoder 的实例,因为目前的案例还比较简单,所以返回 NoOpPasswordEncoder 的实例即可。
3.configure 方法中,我们通过 inMemoryAuthentication 来开启在内存中定义用户,withUser 中是用户名,password 中则是用户密码,roles 中是用户角色。
4. 如果需要配置多个用户,用 and 相连。

3.自定义表单登录页

可以看到自动配置的login页面非常丑,怎么用自己的登陆页面呢?

在SecurityConfig里面配置
1.web.ignoring()是放行静态资源的,不能发静态资源也拦截;
2.anyRequest().authenticated()是表明所有请求都进行身份认证
3.接下来就是配置个性化的登录页面
4.permitAll 表示登录相关的页面/接口不要被拦截
5.最后关闭csrf,这个以后会说

package com.lwj.security01.config;

import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * @Author: lwj
 * @Description: TODO
 * @DateTime: 2021/8/28 14:53
 **/
@Component
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Bean
    PasswordEncoder passwordEncoder(){
        return NoOpPasswordEncoder.getInstance();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/css/**");
    }

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

在这里插入图片描述

附录

login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login</title>
    <link rel="stylesheet" type="text/css" href="css/Login.css"/>
</head>
<body>
<div id="login">
    <h1>Login</h1>
    <form method="post" action="/login.html">
        <input type="text" required="required" placeholder="用户名" name="u"></input>
        <input type="password" required="required" placeholder="密码" name="p"></input>
        <button class="but" type="submit">登录</button>
    </form>
</div>
</body>
</html>

login.css

html{
    width: 100%;
    height: 100%;
    overflow: hidden;
    font-style: sans-serif;
}
body{
    width: 100%;
    height: 100%;
    font-family: 'Open Sans',sans-serif;
    margin: 0;
    background-color: #4A374A;
}
#login{
    position: absolute;
    top: 50%;
    left:50%;
    margin: -150px 0 0 -150px;
    width: 300px;
    height: 300px;
}
#login h1{
    color: #fff;
    text-shadow:0 0 10px;
    letter-spacing: 1px;
    text-align: center;
}
h1{
    font-size: 2em;
    margin: 0.67em 0;
}
input{
    width: 278px;
    height: 18px;
    margin-bottom: 10px;
    outline: none;
    padding: 10px;
    font-size: 13px;
    color: #fff;
    text-shadow:1px 1px 1px;
    border-top: 1px solid #312E3D;
    border-left: 1px solid #312E3D;
    border-right: 1px solid #312E3D;
    border-bottom: 1px solid #56536A;
    border-radius: 4px;
    background-color: #2D2D3F;
}
.but{
    width: 300px;
    min-height: 20px;
    display: block;
    background-color: #4a77d4;
    border: 1px solid #3762bc;
    color: #fff;
    padding: 9px 14px;
    font-size: 15px;
    line-height: normal;
    border-radius: 5px;
    margin: 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值