SpringSecurity身份验证基础入门
对于没有访问权限的用户需要转到登录表单页面。要实现访问控制的方法多种多样,可以通过Aop、拦截器实现,也可以通过框架实现(如:Apache Shiro、Spring Security)。
pom.xml添加依赖

1 <dependency>
2 <groupId>org.springframework.boot</groupId>
3 <artifactId>spring-boot-starter-web</artifactId>
4 </dependency>
5
6 <dependency>
7 <groupId>org.springframework.boot</groupId>
8 <artifactId>spring-boot-starter-thymeleaf</artifactId>
9 </dependency>
10 <dependency>
11 <groupId>org.springframework.boot</groupId>
12 <artifactId>spring-boot-starter-security</artifactId>
13 </dependency>

创建SpringSecurity配置类

1 import org.springframework.beans.factory.annotation.Autowired;
2 import org.springframework.context.annotation.Configuration;
3 import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
4 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
5 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
6 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
7
8 @Configuration
9 @EnableWebSecurity
10 public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
11
12 @Override
13 protected void configure(HttpSecurity http) throws Exception {
14 http
15 .authorizeRequests()
16 .antMatchers("/", "/home").permitAll()
17 .anyRequest().authenticated()
18 .and()
19 .formLogin()
20 .loginPage("/login")
21 .permitAll()
22 .and()
23 .logout()
24 .permitAll();
25 }
26
27 @Autowired
28 public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
29 //inMemoryAuthentication 从内存中获取
30 auth
31 .inMemoryAuthentication()
32 .passwordEncoder(new BCryptPasswordEncoder())
33 .withUser("admin")
34 .password(new BCryptPasswordEncoder()
35 .encode("123456")).roles("USER");
36 }
37 }

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

1 @Controller
2 public class HelloController {
3
4 @RequestMapping("/")
5 public String index() {
6 return "index";
7 }
8
9 @RequestMapping("/hello")
10 public String hello() {
11 return "hello";
12 }
13
14 @RequestMapping(value = "/login", method = RequestMethod.GET)
15 public String login() {
16 return "login";
17 }
18
19 }

index.html

1 <!DOCTYPE html>
2 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
3 xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
4 <head>
5 <title>Spring Security入门</title>
6 </head>
7 <body>
8 <h1>欢迎使用Spring Security!</h1>
9
10 <p>点击 <a th:href="@{/hello}">这里</a> 打个招呼吧</p>
11 </body>
12 </html>

hello.html

1 <!DOCTYPE html>
2 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
3 xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
4 <head>
5 <title>Hello World!</title>
6 </head>
7 <body>
8 <h1 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h1>
9 <form th:action="@{/logout}" method="post">
10 <input type="submit" value="注销"/>
11 </form>
12 </body>
13 </html>

login.html

1 <!DOCTYPE html>
2 <html xmlns="http://www.w3.org/1999/xhtml"
3 xmlns:th="http://www.thymeleaf.org"
4 xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
5 <head>
6 <title>Spring Security Example </title>
7 </head>
8 <body>
9 <div th:if="${param.error}">
10 用户名或密码错
11 </div>
12 <div th:if="${param.logout}">
13 您已注销成功
14 </div>
15 <form th:action="@{/login}" method="post">
16 <div><label> 用户名 : <input type="text" name="username"/> </label></div>
17 <div><label> 密 码 : <input type="password" name="password"/> </label></div>
18 <div><input type="submit" value="登录"/></div>
19 </form>
20 </body>
21 </html>

运行:
打开index.html,点击这里,如果没有登录进入登录页,已登录跳转到hello.html
SpringSecurity身份验证基础入门的更多相关文章
- Spring Boot 5 SpringSecurity身份验证
对于没有访问权限的用户需要转到登录表单页面.要实现访问控制的方法多种多样,可以通过Aop.拦截器实现,也可以通过框架实现(如:Apache Shiro.Spring Security). pom.xm ...
- 无责任Windows Azure SDK .NET开发入门篇二[使用Azure AD 进行身份验证-2.2身份验证开发]
2.2身份验证开发 在我们的案例中,我们是用户通过Web应用程序进行身份识别. 上面的图示说明了如下的一些概念 l Azure AD 是标识提供程序,负责对组织的目录中存在的用户和应用程序的标识进行验 ...
- c# WebApi之身份验证:Basic基础认证
为什么需要身份认证 身份认证是为了提高接口访问的安全性,如果没有身份验证,那么任何匿名用户只要知道服务器的url,就可以随意访问服务器,从而访问或者操作数据库,这会是很恐怖的事. 什么是Basic基础 ...
- 无责任Windows Azure SDK .NET开发入门(二):使用Azure AD 进行身份验证
<編者按>本篇为系列文章,带领读者轻松进入Windows Azure SDK .NET开发平台.本文为第二篇,将教导读者使用Azure AD进行身分验证.也推荐读者阅读无责任Windows ...
- 【asp.net core 系列】13 Identity 身份验证入门
0. 前言 通过前两篇我们实现了如何在Service层如何访问数据,以及如何运用简单的加密算法对数据加密.这一篇我们将探索如何实现asp.net core的身份验证. 1. 身份验证 asp.net ...
- 无责任Windows Azure SDK .NET开发入门篇二[使用Azure AD 进行身份验证]
二.使用Azure AD进行身份验证 之所以将Azure AD 作为开始,是应为基本上我们所有应用都需要进行安全管理.Azure Active Directory (Azure AD) 通过以下方式简 ...
- 【二】shiro入门 之 身份验证
大体步骤如下: 1.首先通过new IniSecurityManagerFactory 并指定一个ini 配置文件来创建一个SecurityManager工厂: 2.接着获取SecurityManag ...
- 《Python编程:从入门到实践》第19章笔记:用户/用户注册/身份验证
接上篇django最基本的一些日常用法,这是第19章笔记,希望在做"动手试一试"的时候可以让自己方便参考. 这一章实现了两个功能: 1.让用户能够添加主题Topic和条目Entry ...
- ASP.NET Core 1.1 静态文件、路由、自定义中间件、身份验证简介
概述 之前写过一篇关于<ASP.NET Core 1.0 静态文件.路由.自定义中间件.身份验证简介>的文章,主要介绍了ASP.NET Core中StaticFile.Middleware ...
随机推荐
- 一、linux概述
1. 学习Linux之前先了解Unix Unix是一个强大的多用户.多任务操作系统.于1969年在AT&T的贝尔实验室开发.UNIX的商标权由国际开放标准组织(The Open Group) ...
- 【Alpha】测试报告
测试中发现的Bug 测试伴随着前后端的首次连接之后与开发一起进行,在本阶段,我们遇到了各种各样的bug,有些体现在功能上,有些虽然不会影响到具体的功能实现,但是会影响到用户体验.由于时间问题,我们对其 ...
- AVR 嵌入式单片机芯片的中断系统介绍
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- [Leetcode 40]组合数和II Combination Sum II
[题目] Given a collection of candidate numbers (candidates) and a target number (target), find all uni ...
- [Leetcode 37]*数独游戏 Sudoku Solver 附解释
[题目] 每一行.每一列.每个3*3的格子里只能出现一次1~9. [思路] 参考了思路,附加了解释. dfs遍历所有非空格子,n是已经填好的个数. 初始化条件.n=81,都填了,返回结束.对于已经填好 ...
- python threading模块中的join()方法和setDeamon()方法的一些理解
之前用多线程的时候看见了很多文章,比较常用的大概就是join()和setDeamon()了. 先说一下自己对join()的理解吧: def join(self, timeout=None): &quo ...
- SpringBoot 2.0 pom.xml 配置(热启动)
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven ...
- Ubuntu16.04下安装OpenCV3.2.0
1.安装官方给的opencv依赖包 $ sudo apt-get install build-essential $ sudo apt-get install cmake git libgtk2.0- ...
- 1、Python2.7编译安装
下载Python安装包(下载地址:https://www.python.org/downloads/) 选择Python 2.7.3版本 防止交互式模式下出现方向键乱码问题,需安装相关包 yum in ...
- wifi探针的使用说明.
我使用的是四博智联提供的WIFI探针 DT-06产品 点击打开链接 1. 数据读取 可以直接通过串口即可读取数据,串口波特率设置为 115200,其它选项默认. 如果需要PC机测试,请使用杜邦线转接到 ...