设为首页 收藏本站
查看: 1612|回复: 0

[资源发布] Spring Security简述

[复制链接]
累计签到:224 天
连续签到:4 天
发表于 2019-11-26 11:46:13 | 显示全部楼层 |阅读模式
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、现在尝试输入有效的用户名和密码:
您现在应该看到我们想要保护的页面。



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-988947-1-1.html 上篇帖子: 没有了 下篇帖子: 没有了
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表