新建项目
在idea中新建一个spring boot项目:
这里我们选择spring web和spring security依赖:如下图
项目需要依赖的包如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.3.4.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.3.4.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.10.RELEASE</version>
<scope>compile</scope>
</dependency>
//使用thymeleaf
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
关于这里为什么使用thymeleaf:
在springboot中如果使用@RestController时候不需要模板技术,但是如果使用@Controller不加入模板技术的话,启动服务器不会报错,但是无法网页访问项目。并且项目的登陆页面使用了html,使用了@Controller,在页面需要返回视图时需要指定使用的视图模板。这里我们使用thymeleaf。
编写表单登录页
将login.html文件放到templates目录下,登录页的代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="/css/style.css" />
<link rel="stylesheet" href="/css/iconfont.css" />
<title>登录界面</title>
</head>
<body>
<div id="bigBox">
<h1>LOGIN</h1>
<form action="/user/login" method="post">
<div class="inputBox">
<div class="inputText">
<span class="iconfont icon-nickname"></span>
<input type="text" name="username" placeholder="Username" />
</div>
<div class="inputText">
<span class="iconfont icon-visible"></span>
<input type="password" name="password" placeholder="Password" />
</div>
</div>
<input class="loginButton" type="submit" value="Login" />
</form>
</div>
</body>
</html>
这里我们需要注意的地方
(1)、input的name属性的值只能时username和password。
(2)、form的action要与Spring Security的配置类中的loginProcessingUrl的参数一致,否则登录页面点击登录时不能登录。
编写WebSecurityConfig类
新建config包,在包内新建Spring Security的配置类WebSecurityConfig类。类中的代码如下:
package com.security.demo.config;
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;
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception{
http.authorizeRequests()
.antMatchers("/user/login","/login.html","/css/**","/img/**","/js/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
//指定登录页的路径
.loginPage("/login.html")
//指定自定义form表单请求的路径
.loginProcessingUrl("/user/login")
.failureUrl("/login?error")
//登录成功后的跳转路径
.defaultSuccessUrl("/success")
//必须允许所有用户访问我们的登录页(例如未验证的用户,否则验证流程就会进入死循环)
//这个formLogin().permitAll()方法允许所有用户基于表单登录访问/login这个page。
.permitAll();
//默认都会产生一个hiden标签 里面有安全相关的验证 防止请求伪造 这边我们暂时不需要 可禁用掉
http .csrf().disable();
}
}
编写TestController类
新建一个controller包,在包中新建一个TestController类,TestController的代码如下所示:
package com.security.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
public class TestController {
@RequestMapping("/login.html")
public String login(){
return "login";
}
@GetMapping("/success")
@ResponseBody
public String success(){
return "login success";
}
}
启动项目
启动项目,控制台自动生成密码,默认用户名为user(这里使用spring security的默认用户,密码在控制台自动生成)。在浏览器访问http://localhost:8080/,将被重定向到http://localhost:8080/login.html。如下所示:
登录成功
自定义的登录页已经显示出来了,现在我们输入用户名user,和控制台复制的密码,点击登录后如下图所示,说明登录成功。
这篇文章对你有帮助吗?作为一名编程爱好者,在评论区留下你的困惑或你的见解,大家一起来交流吧!