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 ...
随机推荐
- 一个简单的windows勒索软件分析
根据分析,此病毒是一个勒索软件,通过修改登录用户密码,留下勒索QQ号码向用户索要金钱. 它调用了Kernel32.dll里的WinExec来执行更改用户密码的cmd命令,密码为107289,更改完密码 ...
- python修炼第三天
今天主要讲了文件操作,函数与装饰器,装饰器比较烧脑,需要多做练习,逐步分解来进行理解! 加油! 一 文件操作 操作系统 提供文件的概念可以操作磁盘. 文件的只读模式: 注意如果是windows ...
- [NOIP2013D1]
T1 Problem 洛谷 Solution 感觉我写的也不是正解... 我是先找出每个循环节的长度l...然后用快速幂求出10 ^ k % l的值.. Code #include<cmath& ...
- sql:按年、月、日钻取时间
#按月排SELECT count(EN_NAME), DATE_FORMAT( CREATE_DATE, "%Y-%m" )FROM financeWHERE DATE_FORMA ...
- Python的进程与线程--思维导图
Python的进程与线程--思维导图
- numpy数据集练习
#1. 安装scipy,numpy,sklearn包 import numpy as np #2. 从sklearn包自带的数据集中读出鸢尾花数据集data from sklearn.datasets ...
- python机器可读数据-csv
逗号分隔值(Comma-Separated Values,CSV) 1 csv数据 还有一种数据类型,叫制表分隔值(tab-separated values,TSV)数据,有时与CSV归为一类. 若文 ...
- 深入java----垃圾回收
Java和C++之间有一睹内存动态分配和垃圾收集技术所围成的“高墙”,墙外面的人想进去,墙里面的人想出来.-------<深入理解JVM虚拟机> 补充:在无用对象判断这两种方法中,都是靠对 ...
- JS-构造函数模式代码实战和总结-极客
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【转】git - 简易指南
原文链接:http://www.bootcss.com/p/git-guide/ 作者:罗杰·杜德勒 感谢:@tfnico, @fhd and Namics 其他语言 english, deutsch ...