新建一个springboot的web项目,我这边只选中了web,创建后以下:html
pom依赖:java
<!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper -->
<!--配置支持jsp-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>8.5.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--添加static和templates的依赖-->
<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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<!-- 因为我使用的spring boot因此我是引入spring-boot-starter-security并且我使用了spring io因此不须要填写依赖的版本号 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
复制代码
以上的jsp依赖若是用不上能够不加哦web
WebSecurityConfigurerAdapter是security中浏览器登陆设置的主类 这里咱们继承后重写如下的三个方法:spring
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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;
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
super.configure(auth);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/hello","/login.html").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
//指定登陆页的路径
.loginPage("/hello")
//指定自定义form表单请求的路径
.loginProcessingUrl("/authentication/form")
.failureUrl("/login?error")
.defaultSuccessUrl("/success")
//必须容许全部用户访问咱们的登陆页(例如未验证的用户,不然验证流程就会进入死循环)
//这个formLogin().permitAll()方法容许全部用户基于表单登陆访问/login这个page。
.permitAll();
//默认都会产生一个hiden标签 里面有安全相关的验证 防止请求伪造 这边咱们暂时不须要 可禁用掉
http .csrf().disable();
}
@Override
public void configure(WebSecurity web) throws Exception {
super.configure(web);
}
}
复制代码
这边咱们指定的登陆页的访问方法为/Hello的方法,这边咱们来编写这个Controller层:apache
@Controller
public class LoginController {
@RequestMapping("/hello")
public String hello() {
//这边咱们,默认是返到templates下的login.html
return "login";
}
}
复制代码
login.html:api
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>第一个HTML页面</title>
</head>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
自定义表单验证:
<!--<form name="f" action="/login" method="post">-->
<form name="f" action="/authentication/form" method="post">
<br/>
用户名:
<input type="text" name="username" placeholder="name"><br/>
密码:
<input type="password" name="password" placeholder="password"><br/>
<input name="submit" type="submit" value="提交">
</form>
</body>
</html>
复制代码
这里值的注意的是表单的用户名name和password输入框的name=""要和security里面的验证的对应:浏览器
name="username";name="password",不然没法识别,另外action="/authentication/form"要与.loginProcessingUrl("/authentication/form")相对应,缘由为:tomcat
因为security是由UsernamePasswordAuthenticationFilter这个类定义登陆的,里面默认是/login路径,咱们要让他用咱们的/authentication/form路径,就须要配置.loginProcessingUrl("/authentication/form")安全
咱们如今启动项目 不管进入哪一个网址都会被拦截返回到登陆页面,以下所示:springboot
这时咱们用户名:user(默认) password:会在启动时候生成 以下:
这个时候咱们登陆就成功了 ,不然不正确会返回到error页面