|
Spring Security,这是一种基于 Spring AOP 和 Servlet 过滤器的安全框架。它提供全面的安全性解决方案,同时在 Web 请求级和方法调用级处理身份确认和授权。
Spring Security为基于Java EE的企业软件应用程序提供全面的安全服务。特别强调支持使用Spring Framework构建的项目,Spring Framework是用于企业软件开发的领先Java EE解决方案。如果您不使用Spring开发企业应用程序, 人们使用Spring Security有很多原因,但是在发现Java EE的Servlet规范或EJB规范的安全特性缺乏典型企业应用程序场景所需的深度之后,大多数人都被这个项目所吸引。虽然提到这些标准,但重要的是要认识到它们不能在WAR或EAR级别上移植。因此,如果切换服务器环境,在新的目标环境中重新配置应用程序的安全性通常需要做很多工作。使用Spring Security克服了这些问题,并为您提供了许多其他有用的,可自定义的安全功能。
您可能知道应用程序安全性的两个主要方面是“身份验证”和“授权”(或“访问控制”)。这是Spring Security目标的两个主要领域。“身份验证”是建立委托人的过程,他们声称是谁(“委托人”通常是指用户,设备或其他可以在您的申请中执行操作的系统。)“授权”是指决定的过程是否允许委托人在您的申请中执行诉讼。为了达到需要授权决定的程度,已经通过认证过程建立了委托人的身份。这些概念很常见,并不是Spring Security特有的。
在身份验证级别,Spring Security支持各种身份验证模型。大多数这些身份验证模型由第三方提供,或由相关标准机构(如Internet工程任务组)开发。此外,Spring Security还提供了一组自己的身份验证功能。
许多独立软件供应商(ISV)采用Spring Security,因为这种灵活的身份验证模型的选择很多。这样做可以让他们快速地将他们的解决方案与他们最终客户的需求集成在一起,而无需进行大量工程或要求客户改变他们的环境。如果上述认证机制都不适合您的需求,Spring Security是一个开放平台,编写自己的认证机制非常简单。Spring Security的许多企业用户需要与不遵循任何特定安全标准的“遗留”系统集成,Spring Security很乐意与这些系统“很好地”合作。
本文以SpringBoot构建一个web项目作为入门项目,介绍Spring Security 的简单应用。(注意:此项目默认你已经熟悉SpringBoot的使用,如果不熟悉可以先去学习SpringBoot的相关知识)
第一步,引入相关jar包
要使用Spring Security,您必须添加必要的依赖项。对于示例,我们将添加以下Spring Security依赖项(我们使用Thymeleaf作为视图模板引擎,需要为Thymeleaf - Spring Security集成模块添加额外的依赖项。):
<dependencies>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.2.11.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.2.11.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
</dependencies>
第二步,创建Spring Security配置
package org.springframework.security.samples.config;
import org.springframework.beans.factory.annotation.Autowired;
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;
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/css/**", "/index").permitAll()
.antMatchers("/user/**").hasRole("USER")
.and()
.formLogin()
.loginPage("/login").failureUrl("/login-error");
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
}
第三步:前端controller
package com.paic.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author WANGYONGQIANG285 2018年12月24日
*
*/
@Controller
public class DemoController {
@RequestMapping({ "/", "index" })
String home() {
return "index";
}
@RequestMapping("/login")
String login() {
return "login";
}
@RequestMapping("/login-error")
String loginError(Model model) {
model.addAttribute("errorMsg", "用户名或密码错误");
model.addAttribute("isError", true);
return "login";
}
@RequestMapping("/user/index")
String userLogin() {
return "user/index";
}
}
第四步:首页
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
<title>Hello Spring Security</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="/css/main.css" th:href="@{/css/main.css}" />
</head>
<body>
<div th:fragment="logout" class="logout" sec:authorize="isAuthenticated()">
Logged in user: <span sec:authentication="name"></span> |
Roles: <span sec:authentication="principal.authorities"></span>
<div>
<form action="#" th:action="@{/logout}" method="post">
<input type="submit" value="Logout" />
</form>
</div>
</div>
<h1>Hello Spring Security</h1>
<p>This is an unsecured page, but you can access the secured pages after authenticating.</p>
<ul>
<li>Go to the <a href="/user/index" th:href="@{/user/index}">secured pages</a></li>
</ul>
</body>
</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-springsecurity4">
<head>
<title>Hello Spring Security</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="/css/main.css" th:href="@{/css/main.css}" />
</head>
<body>
<h1>login Spring Security</h1>
<div th:if="${isError}">
<span th:text="${errorMsg}" style="background-color: red;"></span>
</div>
<div th:fragment="logout" class="login">
<div>
<form action="#" th:action="@{/login}" method="post">
username: <input type = "text" name="username"/> <br/>
password: <input type = "password" name="password"/> <br/>
<input type="submit" value="Login" />
</form>
</div>
</div>
</body>
</html>
第六步:登录成功页面
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Hello Spring Security</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="/css/main.css" th:href="@{/css/main.css}" />
</head>
<body>
<div th:substituteby="index::logout"></div>
<h1>This is a secured page!</h1>
<p><a href="/index" th:href="@{/index}">Back to home page</a></p>
</body>
</html>
第七步:启动服务器并尝试访问、测试http:// localhost:8080 /
1、单击secured pages,系统将提示您登录。
2、对安全应用程序进行身份验证
尝试输入无效的用户名和密码:
3、现在尝试输入有效的用户名和密码:
您现在应该看到我们想要保护的页面。
|
|
|