对于没有访问权限的用户需要转到登录表单页面。要实现访问控制的方法多种多样,可以通过Aop、拦截器实现,也可以通过框架实现(如:Apache Shiro、Spring Security)。

pom.xml添加依赖

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

    &lt;dependency&gt;</br>
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</br>
&lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&gt;</br>
&lt;/dependency&gt;</br>
&lt;dependency&gt;</br>
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</br>
&lt;artifactId&gt;spring-boot-starter-security&lt;/artifactId&gt;</br>
&lt;/dependency&gt;</pre>

创建SpringSecurity配置类

import org.springframework.beans.factory.annotation.Autowired;

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.configuration.EnableWebSecurity;

import
org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration


@EnableWebSecurity


public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override</br>
</span><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">void</span> configure(HttpSecurity http) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception {</br>
http</br>
.authorizeRequests()</br>
.antMatchers(</span>"/", "/home"<span style="color: #000000;">).permitAll()</br>
.anyRequest().authenticated()</br>
.and()</br>
.formLogin()</br>
.loginPage(</span>"/login"<span style="color: #000000;">)</br>
.permitAll()</br>
.and()</br>
.logout()</br>
.permitAll();</br>
}</br> @Autowired</br>
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> configureGlobal(AuthenticationManagerBuilder auth) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception {</br>
auth</br>
.inMemoryAuthentication()</br>
.withUser(</span>"admin").password("123456").roles("USER"<span style="color: #000000;">);</br>
}</br>

}

通过@EnableWebSecurity注解开启Spring Security的功能
继承WebSecurityConfigurerAdapter,并重写它的方法来设置一些web安全的细节
configure(HttpSecurity http)方法,通过authorizeRequests()定义哪些URL需要被保护、哪些不需要被保护。例如以上代码指定了/和/home不需要任何认证就可以访问,其他的路径都必须通过身份验证。
通过formLogin()定义当需要用户登录时候,转到的登录页面。
configureGlobal(AuthenticationManagerBuilder auth)方法,在内存中创建了一个用户,该用户的名称为admin,密码为123456,用户角色为USER。

控制器:

@Controller
public class HelloController {
@RequestMapping(</span>"/"<span style="color: #000000;">)</br>
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String index() {</br>
</span><span style="color: #0000ff;">return</span> "index"<span style="color: #000000;">;</br>
}</br> @RequestMapping(</span>"/hello"<span style="color: #000000;">)</br>
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String hello() {</br>
</span><span style="color: #0000ff;">return</span> "hello"<span style="color: #000000;">;</br>
}</br></br> @RequestMapping(value </span>= "/login", method =<span style="color: #000000;"> RequestMethod.GET)</br>
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String login() {</br>
</span><span style="color: #0000ff;">return</span> "login"<span style="color: #000000;">;</br>
}</br></br>

}

index.html

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"

xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>

<title>Spring Security入门</title>

</head>

<body>

<h1>欢迎使用Spring Security!</h1>

<p>点击 <a th:href="@{/hello}">这里</a> 打个招呼吧</p>


</body>


</html>

hello.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"

xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">

<head>

<title>Hello World!</title>

</head>

<body>

<h1 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h1>

<form th:action="@{/logout}" method="post">

<input type="submit" value="注销"/>

</form>

</body>

</html>

login.html

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml"

xmlns:th="http://www.thymeleaf.org"

xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>

<title>Spring Security Example </title>

</head>

<body>

<div th:if="${param.error}">

用户名或密码错

</div>

<div th:if="${param.logout}">

您已注销成功

</div>

<form th:action="@{/login}" method="post">

<div><label> 用户名 : <input type="text" name="username"/> </label></div>

<div><label> 密 码 : <input type="password" name="password"/> </label></div>

<div><input type="submit" value="登录"/></div>

</form>

</body>

</html>

运行:

打开index.html,点击这里,如果没有登录进入登录页,已登录跳转到hello.html

http://blog.didispace.com/springbootsecurity/

Spring Boot SpringSecurity5 身份验证的更多相关文章

  1. 自定义Spring Security的身份验证失败处理

    1.概述 在本快速教程中,我们将演示如何在Spring Boot应用程序中自定义Spring Security的身份验证失败处理.目标是使用表单登录方法对用户进行身份验证. 2.认证和授权(Authe ...

  2. Kotlin + Spring Boot 请求参数验证

    编写 Web 应用程序的时候,经常要做的事就是要对前端传回的数据进行简单的验证,比如是否非空.字符长度是否满足要求,邮箱格式是否正确等等.在 Spring Boot 中,可以使用 Bean Valid ...

  3. Spring Boot (31) 数据验证

    曾经参数的验证是这样的: public String test(User user){ if(user == null){ throw new NullPointerException("u ...

  4. spring boot+jwt 权限验证

    上周看了一下jwt以前公司的开发都是使用session共享的方法.现在主流的两种方式一种是把登录信息保存在服务器,另一种则是把信息保存在客户端.在使用session 存储的时候会遇到很多的问题,随着项 ...

  5. Spring Boot 表单验证、AOP统一处理请求日志、单元测试

    一.使用@Valid表单验证 于实体类中添加@Min等注解 @Entity public class Girl { @Id @GeneratedValue private Integer id; pr ...

  6. spring boot 表单验证

    1 设置某个字段的取值范围 1.1 取值范围验证:@Min,@Max ① 实例类的属性添加注解@Min ② Controller中传入参数使用@Valid注解 1.2 不能为空验证:@NotNull ...

  7. spring boot中表单验证的使用

    一.前言 为啥子要搞这个表单验证呢?答案简单而现实,举个栗子,你辛辛苦苦的写了一个录入个人信息的功能,比如年龄这个位置,用户就没看到一下子写了个性别男,一提交,直接报错了,是不是很尴尬呢, 作为一个测 ...

  8. 第5章 Spring Boot 功能

    Spring Boot 功能 本节将会介绍Spring Boot的一些细节. 在这里,您可以了解您将要使用和自定义的主要功能. 如果还没有准备好,您可能需要阅读第二部分“入门指南”和第三部分“使用 S ...

  9. 从使用传统Web框架到切换到Spring Boot后的总结

    1.前言 其实我接触 Spring Boot 的时间并不长,所以还算一个初学者,这篇文章也算是我对 Spring Boot 学习以及使用过程中的复盘,如果文章出现描述错误或表达不清晰的地方,欢迎大家在 ...

随机推荐

  1. 洛谷P1628 合并序列

    题目描述 有N个单词和字符串T,按字典序输出以字符串T为前缀的所有单词. 输入输出格式 输入格式: 输入文件第一行包含一个正整数N: 接下来N行,每行一个单词,长度不超过100: 最后一行包含字符串T ...

  2. Java学习之初识线程

    “身之主宰便是心,心之所发便是意,意之本体便是知,意之所在便是物 --摘自阳明先生语录” 1.概念 在说线程之前我们先了解关于进程的一些知识,什么是进程? 程序一旦运行就是一个独立的进程,以windo ...

  3. iview 验证 trigger: 'blur,change', 同时加两个,省的每次还想input 还是 select

    iview 验证 trigger: 'blur,change', 同时加两个,省的每次还想input 还是 select dataRuleValidate: { name: [{ required: ...

  4. 计算机图形学:贝塞尔曲线(Bezier Curve)

    计算机图形学:贝塞尔曲线(Bezier Curve) 贝塞尔能由贝塞尔样条组合而成,也可产生更高维的贝塞尔曲面.

  5. 使用gcc -g编译,gdb调试时仍然存在“no debug symbols found”的错误

    今天为调试一段代码,使用gcc将程序用-g选项重新编译.但是使用gdb进行debug时,仍然出现“no debug symbols found”的错误.仔细检查了一下Makefile,原来后面定义的连 ...

  6. C++的反射

    写得挺不错,支持转帖下 C++语言本身是不支持反射的,但实际应用中总是会有将对象序列化的需求,总不可能C++不支持,我们就不用C++了,既然发明C++的大师们没有考虑这个,那我们只有自己动手了,毛主席 ...

  7. javaEE(2)_http协议

    一.HTTP协议简介 1.客户端连上web服务器后,若想获得web服务器中的某个web资源,需遵守一定的通讯格式,HTTP协议用于定义客户端与web服务器通迅的格式.dos环境下可直接通过telnet ...

  8. 【树状数组 离散化】bzoj1573: [Usaco2009 Open]牛绣花cowemb

    解方程题! Description Bessie学会了刺绣这种精细的工作.牛们在一片半径为d(1 <= d <= 50000)的圆形布上绣花. 它们一共绣了N (2 <= N < ...

  9. jenkins插件开发(二)

    https://wiki.jenkins.io/display/JENKINS/Extend+Jenkins http://commons.apache.org/proper/commons-jell ...

  10. 利用js实现图片展开与收缩

    1.元素居中放大: 1>除了要改变元素的宽高以外,还要改变元素的定位(left,top),如果图片放大一倍,那么位移放大宽高的一半. 2>元素必须是定位的.所以,在css中设置为浮动布局, ...