改变登陆页面
下面这个是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";
}
登陆成功后跳转首页