对于没有访问权限的用户需要转到登录表单页面。要实现访问控制的方法多种多样,可以通过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身份验证基础入门的更多相关文章

  1. Spring Boot 5 SpringSecurity身份验证

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

  2. 无责任Windows Azure SDK .NET开发入门篇二[使用Azure AD 进行身份验证-2.2身份验证开发]

    2.2身份验证开发 在我们的案例中,我们是用户通过Web应用程序进行身份识别. 上面的图示说明了如下的一些概念 l Azure AD 是标识提供程序,负责对组织的目录中存在的用户和应用程序的标识进行验 ...

  3. c# WebApi之身份验证:Basic基础认证

    为什么需要身份认证 身份认证是为了提高接口访问的安全性,如果没有身份验证,那么任何匿名用户只要知道服务器的url,就可以随意访问服务器,从而访问或者操作数据库,这会是很恐怖的事. 什么是Basic基础 ...

  4. 无责任Windows Azure SDK .NET开发入门(二):使用Azure AD 进行身份验证

    <編者按>本篇为系列文章,带领读者轻松进入Windows Azure SDK .NET开发平台.本文为第二篇,将教导读者使用Azure AD进行身分验证.也推荐读者阅读无责任Windows ...

  5. 【asp.net core 系列】13 Identity 身份验证入门

    0. 前言 通过前两篇我们实现了如何在Service层如何访问数据,以及如何运用简单的加密算法对数据加密.这一篇我们将探索如何实现asp.net core的身份验证. 1. 身份验证 asp.net ...

  6. 无责任Windows Azure SDK .NET开发入门篇二[使用Azure AD 进行身份验证]

    二.使用Azure AD进行身份验证 之所以将Azure AD 作为开始,是应为基本上我们所有应用都需要进行安全管理.Azure Active Directory (Azure AD) 通过以下方式简 ...

  7. 【二】shiro入门 之 身份验证

    大体步骤如下: 1.首先通过new IniSecurityManagerFactory 并指定一个ini 配置文件来创建一个SecurityManager工厂: 2.接着获取SecurityManag ...

  8. 《Python编程:从入门到实践》第19章笔记:用户/用户注册/身份验证

    接上篇django最基本的一些日常用法,这是第19章笔记,希望在做"动手试一试"的时候可以让自己方便参考. 这一章实现了两个功能: 1.让用户能够添加主题Topic和条目Entry ...

  9. ASP.NET Core 1.1 静态文件、路由、自定义中间件、身份验证简介

    概述 之前写过一篇关于<ASP.NET Core 1.0 静态文件.路由.自定义中间件.身份验证简介>的文章,主要介绍了ASP.NET Core中StaticFile.Middleware ...

随机推荐

  1. SVN删除文件和恢复文件

    SVN删除文件 一.本地删除SVN删除文件中的本地删除,指的是在客户端delete了一个文件,但还没有commit,使用revert来撤销删除. 二.服务器删除1.通过本地删除后提交服务器a)Upda ...

  2. 7ci

  3. shell脚本实现git和svn统计log代码行

    实现的功能 git 根据传入的三个参数:起始统计日期.结束统计日期.git仓库地址. 脚本统计的是git仓库内的所有分支的log信息. 脚本统计的是指定时间段内.每一个提交人指定的git地址的所有分支 ...

  4. type-of-python作业-判断字符串是否属于回文需要忽略其中的标点、空格与大小写

    type-of-python作业 作业练习:要想检查文本是否属于回文需要忽略其中的标点.空格与大小写.例如,"Rise to vote, sir."是一段回文文本,但是我们现有的程 ...

  5. Excel 使用单元格的值 查询MySQL数据库并返回数据给相应的单元格

    Dim MyConn As ObjectPrivate Sub ConnectDB()Set MyConn = CreateObject("ADODB.Connection")   ...

  6. Linux 操作系统目录结构

    /  根目录 # ls /bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp  var bin - ...

  7. KendoUi 学习笔记一

    本系列主要是记录KendoUI的学习过程. KendoUi的特点有以下特点: 1. 70+UI控件 控件有DataGrids,DropDowns,Menus和Buttons,还有一些商业的控件,比如C ...

  8. 看到一个简单的背单词java程序的设计,收藏下

    https://blog.csdn.net/qq_40605167/article/details/81023836

  9. jmeter下载和配置

    一.下载 1.进入官网:http://jmeter.apache.org/ 3.环境变量相关配置 电脑桌面---->“计算机”图标---->鼠标右键选择“属性”---->点击高级系统 ...

  10. nginx 配置 HTTPS 及http 强制跳转https.

    #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...