SpringSecurity初体验
1.新建一个SpringBoot项目,添加Web和SpringSecurity依赖
2.写一个接口进行测试
当我们直接启动这个项目,访问这个
http://localhost:8080/hello
接口时,发现会重定向到一个login页面
而默认的用户就是user,密码则是启动项目时,console栏生成的一串UUID
键入用户名和密码,则可以正常访问这个接口
由上述操作可以知道这些都是自动配置的,是一种约定,但是每次启动都会生成不一样的密码,较为麻烦,如何配置,让生成的密码不变,有两种方法
2.1.在application.yml中进行配置
2.2增加一个配置类![在这里插入图片描述](https://img-blog.csdnimg.cn/0abe5f3b02c649a8b3d4698147b5a8d3.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6JKf6JK7KzE=,size_20,color_FFFFFF,t_70,g_se,x_16)
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;
}