您的位置:首页 > 编程语言 > Java开发

spring security 3.X 入门例子

2012-06-05 14:05 267 查看
昨天建了一个spring security demo 结果跑了一下午没搞定,一直报错误!网上找了好多的资料都不是入门级别的,而且都是他们自己的理解、

今天上午尝试了好多次,最终把bug 消灭掉!所以总结一下spring security入门需要注意的地方。

1、spring security 你可以到spring官方下载到、解压出来里面有war的例子的,你可以放到你的tomcat 的webapp下,然后启动你的

webapp,会解压出来你也可以试着跑一下!这里不再赘述,我们解压的目的是为了获得spring security的所需的jar文件(到他的例子的lib下

copy出spring security所需的jar,记住全部copy)。

2.然后我们就是新建一个applicationContext-security.xml文件(这里需要他的头文件,直接到你解压出来的里面copy) ,然后配置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、配置好这些你或许你想该部署项目了,如果你启动服务器了,噩梦就开始了会抱出如下的错误!

SEVERE: Exception starting filter springSecurityFilterChain

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined

at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:387)

at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:971)

at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:246)

at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)

at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:168)

at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:884)

at org.springframework.web.filter.DelegatingFilterProxy.initDelegate(DelegatingFilterProxy.java:216)

at org.springframework.web.filter.DelegatingFilterProxy.initFilterBean(DelegatingFilterProxy.java:145)

at org.springframework.web.filter.GenericFilterBean.init(GenericFilterBean.java:179)

Clearly the problem is that we haven’t configured any bean named ‘springSecurityFilterChain’.

笔者下午就出现这个问题,不解看了看网上的解决方案,结果都说是神马jar文件冲突,或者applicationContext-security.xml文件配置问题

要不就是监听顺序问题,要么就是springSecurityFilterChain 这个的s大写了!可是笔记看了都是正确的,实在没办法看官方的吧!都是公说公有理

婆说婆有理,那些问题可能会有,但是排查完没有错误了!看了官方文档说是applicationContext-security.xml必须有默认的配置的,就加上了

如下片段:

<http auto-config="true">

<intercept-url pattern="/**" access="ROLE_USER" />

</http>

这个时候大家该想这个时候没问题了吧!不错,笔者也是这样想的,可是启动后噩梦又来了,还是报错!

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: No AuthenticationEntryPoint could be established. Please make sure you have a login mechanism configured through the namespace
(such as form-login) or specify a custom AuthenticationEntryPoint with the custom-entry-point-ref attribute

Offending resource: ServletContext resource [/WEB-INF/applicationContext-security.xml]

at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)

at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)

at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:72)

So spring security is failing fast as it expects us to have some form of authentication mechanism:

spring security有一套严格的安全验证授权的机制,上面的才是实现安全配置

只有拥有ROLE_USER 角色的用户才能访问。<http>元素是所有web 相关的命名空间功能的上级元素(这里不再赘述只是入门demo)

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>元素来定义不同的认证数据, 每个会被需要时使用。

现在,你可以启动程序,然后就会进入登录流程了。

这个时候当你请求任何一个你项目下的资源的时候spring security自动生成一个login只有你根据你配置的user登陆才会正常访问了!

这个时候你的spring security demo 版就成功了!

以后就是深入根据官方文档spring security的强大功能就会显现出来!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: