Spring Security 快速入门(1)

一.背景:

在一个项目中,我们有很多功能,例如增删改查等基本功能,还有很多额外的扩展功能。在生产环境下我们如果不登录后台系统就可以完成这些功能操作吗?案显然是否定的,要操作这些功能必须首先登录到系统才可以。:是不是所有用户,只要登录成功就都可以操作所有功能呢?答案是否定的,并不是所有的用户都可以操作这些功能。不同的用户可能拥有不同的权限,这就需要进
行授权了。

1.授权与认证的理解:

认证:系统提供的用于识别用户身份的功能,通常提供用户名和密码进行登录其实就是在进行认证,认
证的目的是让系统知道你是谁。
授权:用户认证成功后,需要为用户授权,其实就是指定当前用户可以操作哪些功能。
本章节就是要对后台系统进行权限控制,其本质就是对用户进行认证和授权

授权与权限的建立需要涉及到4个实体(项目需要):

  • 权限Permission
  • 角色Role
  • 用户User
  • 菜单Menu
    这几个实体之间存在着多对多的关系,故需要建立7张基础表,如下:
    用户表t_user、权限表t_permission、角色表t_role、菜单表t_menu、用户角色关系表t_user_role、角色权限关系表t_role_permission、角色菜单关系表t_role_menu。

Spring Security是 Spring提供的安全认证服务的框架。 使用Spring Security可以帮助我们来简化认证
和授权的过程。官网:Spring Security

二.入门案例

1.spring依赖导入:

<dependency>
	 <groupId>org.springframework.security</groupId>
	 <artifactId>spring-security-web</artifactId>
	 <version>5.0.5.RELEASE</version>
</dependency>
<dependency> 
	   <groupId>org.springframework.security</groupId>
	   <artifactId>spring-security-config</artifactId> 
	   <version>5.0.5.RELEASE</version>
</dependency>

当然还要导入spring-context的依赖,因为Spring Security是基于spring的。
因为我们要使用tomcat,所以:

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <port>85</port>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>

2.创建index.html

我们先创建一个页面index.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    hello spring security
</body>
</html>

后面我们使用权限框架,在没有认证(没有输入用户名、密码)的情况下,我们无法访问该页面

3.配置web.xml (整合spring security、加载spring配置文件)

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <filter>
    <!--
      DelegatingFilterProxy用于整合第三方框架
      整合Spring Security时过滤器的名称必须为springSecurityFilterChain,
	  否则会抛出NoSuchBeanDefinitionException异常
    -->
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载 -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-security.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

4.创建spring-security配置文件(配置认证、授权等关系)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:security="http://www.springframework.org/schema/security"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
						http://www.springframework.org/schema/beans/spring-beans.xsd
						http://www.springframework.org/schema/mvc
						http://www.springframework.org/schema/mvc/spring-mvc.xsd
						http://code.alibabatech.com/schema/dubbo
						http://code.alibabatech.com/schema/dubbo/dubbo.xsd
						http://www.springframework.org/schema/context
						http://www.springframework.org/schema/context/spring-context.xsd
                     http://www.springframework.org/schema/security
                     http://www.springframework.org/schema/security/spring-security.xsd">

<!--    http:用于定义相关权限控制 auto-config:是否自动配置 设置为true时框架会提供默认的一些配置,例如提供默认的登录页面、登出 处理等 设置为false时需要显示提供登录表单配置,否则会报错 use-expressions:用于指定intercept-url中的access属性是否使用表达式 &ndash;&gt;-->
    <security:http auto-config="true" use-expressions="true">
        <!--intercept-url:定义一个拦截规则 pattern:对哪些url进行权限控制 access:在请求对应的URL时需要什么权限,默认配置时它应该是一个以逗号分隔的角色列 表, 请求的用户只需拥有其中的一个角色就能成功访问对应的URL -->
        <security:intercept-url pattern="/**" access="hasRole('ROLE_ADMIN')" />
    </security:http>
    <!--authentication-manager:认证管理器,用于处理认证操作 -->
    <security:authentication-manager>
        <!--authentication-provider:认证提供者,执行具体的认证逻辑 -->
        <security:authentication-provider>
            <!--user-service:用于获取用户信息,提供给authentication-provider进行认证 -->
            <security:user-service>
                <!--user:定义用户信息,可以指定用户名、密码、角色,后期可以改为从数据库查询 用户信息 {noop}:表示当前使用的密码为明文 -->
                <security:user name="admin" password="{noop}admin" authorities="ROLE_ADMIN"></security:user>
            </security:user-service>
        </security:authentication-provider> </security:authentication-manager> 
</beans>

接下来我们install项目,并启动tomcat,访问localhost:85/index.html,会自动出现下面的页面(这是spring Security默认提供的认证界面):
在这里插入图片描述
我们先输入错误的User和password看看会怎么样?
在这里插入图片描述
然后我们再次输入正确的user和password:
在这里插入图片描述

在这里插入图片描述
可以成功访问到我们设定的页面了!!

可见我们Spring Security主要是保护了我们的文件,我们在配置文件中设定了认证角色和授权的关系,在登录页面只要我们以正确合法的角色进行登入,就能进入到指定的页面

以上是我们Spring Security的小测试,但是无法达到项目制作的要求,请看第二篇文章,讲解Spring Security的更多功能,以便达到项目要求,并提供在大多数Spring Security的编写模板:Spring Security 快速入门(2)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值