博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring security 3.X 入门例子
阅读量:4186 次
发布时间:2019-05-26

本文共 1467 字,大约阅读时间需要 4 分钟。

1、简介

       今天新建了一个spring security demo,但是出现了很多错误,现将 spring security 3.X 的使用记录下,以避免再次出现错误。

      1)spring security 你可以到spring官方下载到、解压出来里面有war的例子的,获得spring security的所需的jar文件.

      2)然后我们就是新建一个applicationContext-security.xml文件,然后配置web.xml

      装载配置文件
      <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                     classpath:applicationContext-security.xml
              </param-value>
      </context-param>
      配置文件监听器
      <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      spring security的过滤连,官方叫钩子。
      <filter>
          <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) applicationContext-security.xml必须有默认的配置的,在其中加上了如下片段:
      <http auto-config="true">
              <intercept-url pattern="/**" access="permitAll" requires-channel="http"/>
        </http> 
       spring security有一套严格的安全验证授权的机制。<http>元素是所有web 相关的命名空间功能的上级元素

      4)在applicationContext-security.xml:

      <authentication-manager>
 
              <authentication-provider>
              <user-service>
                      <user name="dobby" password="dobby" authorities="ROLE_USER, ROLE_ADMIN" />
                      <user name="xi" password="xi" authorities="ROLE_USER" />
              </user-service>
              </authentication-provider>
        </authentication-manager>
      这段代码非常重要使用<authentication-provider>元素意味着用户信息将被认证管理用作处理认证请求,你可以拥有多个<authentication-provider>元素来定义不同的认证数据, 每个会被需要时使用。
      现在,你可以启动程序,然后就会进入登录流程了。
参考文献:

转载地址:http://kmdoi.baihongyu.com/

你可能感兴趣的文章
大数据折射算法“歧视”?王思聪微博抽奖113位,仅有一位男性
查看>>
Java、C、C+ +、PHP、Python分别用来开发什么?一篇文章告诉你!
查看>>
Linux-ACL权限介绍
查看>>
Linux -文件系统
查看>>
Linux常用命令-进程管理
查看>>
Linux - 定时任务
查看>>
Linux - SHELL基础
查看>>
Linux-SHELL基础语法
查看>>
Linux-SHELL变量
查看>>
Linux-SHELL常用命令
查看>>
Linux-网络运维基础
查看>>
Linux网络运维-SSH
查看>>
Linux网络运维 -- 配置DHCP服务器
查看>>
Android开发问题记录
查看>>
Verilog编程网站学习——门电路、组合电路、时序电路
查看>>
android——学生信息显示和添加
查看>>
Android——ImageSwitcher轮流显示动画
查看>>
Android——利用手机端的文件存储和SQLite实现一个拍照图片管理系统
查看>>
图像调优1:清晰度相关参数MTF,SFR,MTF50,MTF50P 以及TVL的概念以及换算说明
查看>>
图像调优2:什么是10° D65 和 2° D65
查看>>