本文共 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/