zoukankan      html  css  js  c++  java
  • 006-SpringSecurity权限怎么引入项目

    1 认证与授权

    什么是认证?

    认证只需要用到用户表就行了,认证就是让系统知道你是谁,所以知道你的用户名和密码都正确就可以。

    那么,什么是授权呢?

    用户必须完成认证之后,才可以授权。

    权限认证与授权有经典的7张表

    1. 用户表
    2. 角色表
    3. 用户和角色中间表
    4. 菜单表
    5. 用户和菜单中间表
    6. 权限表
    7. 角色和权限中间表

    • [ ] 首先根据用户来查询角色
    • [ ] 再根据角色查询对应的菜单
    • [ ] 根据角色查找对应的权限
    • [ ] 最后都封装到用户这个对象中,就拿到了这个用户的所有信息

    2 权限的框架

    常用的权限框架除了springsecurity,还有一个相对简单的是apache shiro框架

    那么,怎么使用呢,请看第3


    3 框架的使用

    3-1 导入maven坐标

    <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-config</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-taglibs</artifactId>
    </dependency>
    

    3-2 在web.xml中整合权限框架

    全部的web.xml如下:

    <!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>
        <!--整合spring security时,此过滤器的名称固定springSecurityFilterChain-->
        <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>
    
    
      <!-- 解决post乱码拦截器 -->
      <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
          <param-name>encoding</param-name>
          <param-value>utf-8</param-value>
        </init-param>
        <init-param>
          <param-name>forceEncoding</param-name>
          <param-value>true</param-value>
        </init-param>
      </filter>
    
      <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
    
    
      <!--加载Springmvc-->
      <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:springmvc.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>
    
    

    上面是web.xml中的配置文件,其中

    <!--委派过滤器拦截,用于整合其他框架-->
    
      <filter>
        <!--整合spring security时,此过滤器的名称固定springSecurityFilterChain-->
        <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>
    

    3-3 配置springsecurity.xml文件

    完整的内容如下,稍后再解释

    <?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">
    
        <!--配置哪些资源匿名可以访问(不登录也能访问)-->
        <security:http security="none" pattern="/login.html"></security:http>
        <security:http security="none" pattern="/css/**"></security:http>
        <security:http security="none" pattern="/img/**"></security:http>
        <security:http security="none" pattern="/js/**"></security:http>
        <security:http security="none" pattern="/plugins/**"></security:http>
    
    
        <!--
            auto-config:自动配置,如果设置为true,表示自动应用一些默认配置,比如框架会提供一个默认的登录页面
            use-expressions:是否使用spring security提供的表达式来描述权限
        -->
        <security:http auto-config="true" use-expressions="true">
            <security:headers>
                <!--设置在页面可以通过iframe访问受保护的页面,默认为不允许访问-->
                <security:frame-options policy="SAMEORIGIN"></security:frame-options>
            </security:headers>
    
    
    
            <!--
                pattern:描述拦截规则,配置拦截规则,/** 表示拦截所有请求
                asscess:指定所需的访问角色或者访问权限
            -->
            <!--只要认证通过就可以访问-->
            <security:intercept-url pattern="/pages/**"  access="isAuthenticated()" />
    
            <!--如果我们要使用自己指定的页面作为登录页面,必须配置登录表单.页面提交的登录表单请求是由框架负责处理-->
            <!--
                login-page:指定登录页面访问URL
            -->
            <security:form-login
                    login-page="/login.html"
                    username-parameter="username"
                    password-parameter="password"
                    login-processing-url="/login.do"
                    default-target-url="/pages/main.html"
                    authentication-failure-url="/login.html"></security:form-login>
    
            <!--
              csrf:对应CsrfFilter过滤器
              disabled:是否启用CsrfFilter过滤器,如果使用自定义登录页面需要关闭此项:disabled="true",否则登录操作会被禁用(403)
            -->
            <security:csrf disabled="true"></security:csrf>
    
            <!--
              logout:退出登录
              logout-url:退出登录操作对应的请求路径
              logout-success-url:退出登录后的跳转页面
            -->
            <security:logout logout-url="/logout.do"
                             logout-success-url="/login.html" invalidate-session="true"/>
    
        </security:http>
    
    
    
        <!--配置认证管理器-->
        <security:authentication-manager>
            <!--配置认证提供者-->
            <security:authentication-provider user-service-ref="springSecurityUserService">
    
                <!--指定对密码进行加密的对象-->
                <security:password-encoder ref="passwordEncoder"></security:password-encoder>
            </security:authentication-provider>
        </security:authentication-manager>
    
        <!--配置密码加密对象-->
        <bean id="passwordEncoder"
              class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" />
    
        <!--开启注解方式权限控制-->
        <security:global-method-security pre-post-annotations="enabled" />
    
    
    </beans>
    

    3-4 在工程中定义类并实现SpringSecurityUserService接口

    在工程中定义类并实现SpringSecurityUserService接口

    这一步很重要。

    需要重写这个方法,这个方法的作用就是根据用户名查询用户的信息,

    其中,查询出来的用户信息就包含用户的权限。

    这个方法是什么呢?

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException{
        根据用户名查询用户
    }
    

    3-5 引入Spring security文件

    在Springmvc中引入引入Spring security文件

    <import resource="Spring security.xml"></import>
    

    4 开始权限控制

    这时候就可以在controller方法上加上权限控制的注解

    比如下面这段代码:

    删除需要有删除的权限

    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")

    //删除检查项
        @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")//权限校验
        @RequestMapping("/delete")
        public Result delete(Integer id){
            try{
                checkItemService.deleteById(id);
            }catch (Exception e){
                e.printStackTrace();
                //服务调用失败
                return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
            }
            return  new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        }
    
  • 相关阅读:
    LeetCode 18. 4Sum (四数之和)
    开运算、闭运算、形态学梯度、顶帽、黑帽
    膨胀和腐蚀
    四种滤波方式
    关于平滑处理及图像滤波与滤波器
    27、简述redis的有哪几种持久化策略及比较?
    26、redis中默认有多少个哈希槽?
    25、如何实现redis集群?
    大聊Python----Select解析
    大聊Python----IO口多路复用
  • 原文地址:https://www.cnblogs.com/hellosiyu/p/13733140.html
Copyright © 2011-2022 走看看