`
bukebuhao
  • 浏览: 88439 次
  • 性别: Icon_minigender_1
  • 来自: 绍兴
社区版块
存档分类
最新评论

spring security3使用笔记

阅读更多

 

 

先对项目中用到的security进行总结如下:

  • sample

      熟悉一个example,了解一些security的运行机制

      例如参照spring-security-samples-tutorial-3.1.x.war,直接放到tomcat下webapps下

  • sample 集成到自己的项目中去

      这一步也很简单,就是把sample的配置拷贝到自己的项目中去。web.xml的配置添加进去,追加applicationContext-security.xml文件,只需简单地修改一下intercept-url,也可不修改,都拷贝过去。

  • 改造applicationContext-security

      由于例子中,使用的都是自动生成的login页面,而且用户信息是在配置文件中指定的,因此需要改造。我主要是从两方面去改造的。首先修改成自己的登录页,其次,从数据库中获取用户信息。

 

     如何修改成自己的登录login页呢?我主要参考了http://www.blogjava.net/youxia/archive/2008/12/07/244883.html,对整个security框架有了个整体了解,其次阅读了http://www.blogjava.net/SpartaYew/archive/2011/06/15/350630.html对security的不同配置方案,了解到security的如何配置。最终我还是查找的springsecurity官方pdf,了解<form-login>元素使用,配置login-page和login-processing-url就可替换成自己的login,default-target-url属性是自动跳转的页面。到目前为止感觉有点乱,实际上很简单的,就是整个form提交的流程,首先是login页面,用户输入信息,其次是login提交的处理的url,就是action的路径,其次就是处理成功跳转和处理失败跳转的页面。因此具体的配置属性可参考文档,如果你的是myeclipse,把鼠标放到<form-login>,可通过properties视图查看到所有的可配置属性,同理退出也需要自定义的页面。

 

     接下来就是如何从数据库读取用户信息?从以上两个网址上已经了解到只要修改UserDetailService,也就是user-service,修改的方式很多种,我直接选择的sql语句。这点我是从http://download.csdn.net/detail/klitao/2764850下载例子,各种各样的配置的例子都有。这样基本的配置基本搞定。参照代码如下:

<http use-expressions="true">

		<intercept-url pattern="/login.html" access="permitAll" />
		<intercept-url pattern="/loginprocess.html" access="permitAll" />
		<intercept-url pattern="/welcome.html" access="isAuthenticated()" />
		<intercept-url pattern="/ad/**" access="hasRole('ROLE_SYSTEM')" />
		<intercept-url pattern="/account/**" access="hasRole('ROLE_SYSTEM')" />
		<intercept-url pattern="/picc/**"
			access="hasAnyRole('ROLE_SYSTEM','ROLE_PICC')" />
		<intercept-url pattern="/**" access="denyAll" />
		<form-login login-page="/login.html"
			authentication-failure-url="/login.html" default-target-url="/"
			login-processing-url="/loginprocess.html" />
		<logout logout-success-url="/login.html" logout-url="/logout.html" />
		<remember-me />

		<session-management>
			<concurrency-control max-sessions="1"
				error-if-maximum-exceeded="true" />
		</session-management>

	</http>

	
	<authentication-manager>
		<authentication-provider user-service-ref="scskUserDetailsService">
			<password-encoder hash="md5">
				<salt-source user-property="username" />
			</password-encoder>
		</authentication-provider>
	</authentication-manager>
	
	<beans:bean id="scskUserDetailsService"
		class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl">
		<beans:property name="dataSource" ref="dataSourceOrcl" />
		<beans:property name="usersByUsernameQuery"
			value="select t.account, t.password, decode(status, 0, 1, 0) as status from U_USERS t where t.account = ?" />
		<beans:property name="authoritiesByUsernameQuery"
			value="select t.account, t.user_role from U_USERS t where t.account = ?" />
	</beans:bean>
 

 

到目前为止,基本搞定security的简单配置。但是总感觉有点云里雾里的感觉,文档写的太零散,只能做手册查询了。然后到网上搜,总算发现了一本比较的security3书,请参考http://lengyun3566.iteye.com/blog/1068998,这里是中文的翻译,书写得也很好,翻译的也很不错的。大家自己认真读下,我保证收获颇丰!这样就会对security3有个完整清晰的认识!以后想扩展想改造,就不会摸石头过河啦!另外登录页追加验证参照http://www.iteye.com/topic/720867,我的配置如下

<http use-expressions="true" entry-point-ref="authenticationProcessingFilterEntryPoint">
		<intercept-url pattern="/login.html" access="permitAll" />
		<intercept-url pattern="/loginprocess.html" access="permitAll" />
		<intercept-url pattern="/upload.html" access="permitAll" />
		<intercept-url pattern="/public/**" access="permitAll" />
		<intercept-url pattern="/editUpload.html" access="permitAll" />
		<intercept-url pattern="/ad/**" access="hasRole('ROLE_SYSTEM')" />
		<intercept-url pattern="/account/**" access="hasRole('ROLE_SYSTEM')" />
		<intercept-url pattern="/picc/**"
			access="hasAnyRole('ROLE_SYSTEM','ROLE_PICC')" />
		<intercept-url pattern="/**" access="isAuthenticated()" />
		
		<custom-filter ref="scskUserLoginFilter" position="FORM_LOGIN_FILTER" />
		<!--  
		<form-login login-page="/login.html"
			authentication-failure-url="/login.html" default-target-url="/"
			login-processing-url="/loginprocess.html" />
		-->
		<logout logout-success-url="/login.html" logout-url="/logout.html" />
		<remember-me />
		
		<session-management>
			<concurrency-control max-sessions="1"
				error-if-maximum-exceeded="false" />
		</session-management>

	</http>

	<beans:bean id="authenticationProcessingFilterEntryPoint"
		class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
		<beans:property name="loginFormUrl" value="/login.html"></beans:property>
	</beans:bean>
	<beans:bean id="scskUserLoginFilter" class="com.ccc.scsk.filter.UserLoginFilter">
		<beans:property name="filterProcessesUrl" value="/loginprocess.html"></beans:property>
		<beans:property name="authenticationSuccessHandler"
			ref="loginLogAuthenticationSuccessHandler"></beans:property>
		<beans:property name="authenticationFailureHandler"
			ref="simpleUrlAuthenticationFailureHandler"></beans:property>
		<beans:property name="authenticationManager" ref="authenticationManager"></beans:property>
	</beans:bean>
	<beans:bean id="loginLogAuthenticationSuccessHandler"
		class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
		<beans:property name="defaultTargetUrl" value="/"></beans:property>
	</beans:bean>
	<beans:bean id="simpleUrlAuthenticationFailureHandler"
		class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
		<beans:property name="defaultFailureUrl" value="/login.html"></beans:property>
	</beans:bean>


	<authentication-manager alias="authenticationManager">
		<authentication-provider user-service-ref="scskUserDetailsService">
			<password-encoder hash="md5">
				<salt-source user-property="username" />
			</password-encoder>
		</authentication-provider>
	</authentication-manager>

	<beans:bean id="scskUserDetailsService"
		class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl">
		<beans:property name="dataSource" ref="dataSourceOrcl" />
		<beans:property name="usersByUsernameQuery"
			value="select t.account, t.password, decode(status, 0, 1, 0) as status from U_USERS t where t.account = ?" />
		<beans:property name="authoritiesByUsernameQuery"
			value="select t.account, t.user_role from U_USERS t where t.account = ?" />
	</beans:bean>
	
	 <beans:bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <beans:property name="basename" value="org/springframework/security/messages" />
    </beans:bean>
 

       总之,参考文档,参考实例,多阅读相关的文档,多动手就可搞定的,不过,一定要把基础打牢的,最起码要熟悉一般的流程,对验证的基本流程要熟悉的,推荐要熟悉http://lengyun3566.iteye.com/blog/1103107内容

http://lengyun3566.iteye.com/blog/1103107 写道
推荐学习security的必备书

 

 

 

 

分享到:
评论
1 楼 lzlun129 2015-01-07  
非常感谢博主!提供的学习路线对我来说帮助太大了!

相关推荐

    springsecurity学习笔记

    在"springsecurity学习笔记"中,你可能会涉及以下主题: - Spring Security的基本配置,包括web安全配置和全局安全配置。 - 如何自定义认证和授权流程,比如实现自定义的AuthenticationProvider和...

    SpringSecurity笔记,编程不良人笔记

    在本笔记中,我们将深入探讨SpringSecurity的核心概念、配置以及如何与SpringBoot结合使用。 1. **SpringSecurity核心概念** - **Filter Chain**: SpringSecurity通过一系列过滤器实现其安全功能,这些过滤器构成...

    spring security3笔记

    《Spring Security 3笔记》 在深入探讨Spring Security 3的知识点之前,我们先了解下这个框架的基本概念。Spring Security是Spring生态系统中的一个组件,它为Java应用提供了全面的安全服务,包括认证、授权以及Web...

    Spring Security OAuth2.0学习笔记.zip

    Spring Security OAuth2.0学习笔记 什么是认证、授权、会话。 Java Servlet为支持http会话做了哪些事儿。 基于session认证机制的运作流程。 基于token认证机制的运作流程。 理解Spring Security的工作原理,Spring ...

    Spring Security 资料合集

    这三份资料——"实战Spring Security 3.x.pdf"、"Spring Security 3.pdf" 和 "Spring Security使用手册.pdf" 将深入探讨这些概念,并提供实践指导,帮助读者掌握如何在实际项目中应用Spring Security。通过学习这些...

    Spring Security笔记.rar

    本笔记将深入探讨Spring Security的核心概念、配置以及如何在实际项目中应用。 一、Spring Security核心概念 1. **身份验证(Authentication)**:这是验证用户身份的过程。Spring Security提供了多种方式来实现,...

    Spring Security tutorial 学习笔记(一)

    在 `SpringSecurityTest01` 这个压缩包文件中,你可能找到了一个示例项目,它可能包含了配置类、控制器、视图和测试用例。通过分析这些代码,你可以更深入地理解如何在实际项目中应用 Spring Security。 总之,...

    springsecurity3 学习笔记源码分析所得

    Spring Security 是一个强大的Java安全框架,用于...总的来说,Spring Security 3的学习笔记和源码分析对提升安全开发技能大有裨益,不仅可以加深理论理解,还能在实际项目中灵活运用,构建更加健壮、安全的应用系统。

    SpringSecurity笔记2-SpringSecurity命名空间

    在本笔记中,我们将深入探讨Spring Security的命名空间,这是配置该框架的关键部分。通过理解这些命名空间,开发者可以更有效地控制应用程序的安全策略。 首先,Spring Security的核心配置是通过XML命名空间完成的...

    狂神Spring Security静态资源

    3. **使用Spring Security的访问决策管理器(AccessDecisionManager)**:如果你希望控制哪些角色可以访问静态资源,可以实现自己的访问决策管理器,然后在配置中指定使用这个管理器。 4. **使用HTTP基本认证或OAuth2...

    Spring Security 笔记思维导图

    Spring Boot 整合 Spring Security 包含认证,授权,加密,验证码,前后端分离,记住密码,自定义组件等

    视频配套笔记_Spring Security OAuth2.0认证授权_v1.1.rar

    Spring Security OAuth2.0 是一个广泛使用的Java安全框架,它为构建安全的Web应用程序提供了强大的支持。OAuth2.0是授权框架的一个标准,允许第三方应用在用户授权的情况下访问其私有资源,而无需共享用户的登录凭证...

    Spring学习笔记+学习源码.zip

    3. **面向切面编程(Aspect-Oriented Programming,AOP)**:Spring AOP允许开发者定义“切面”——跨越多个类的行为或责任。通过切点(Pointcut)和通知(Advice),可以在不修改原有代码的情况下添加新的功能。 4...

    spring security学习笔记

    spring security学习笔记

    springboot+springsecurity入门

    在这个"springboot+springsecurity入门"项目中,我们将关注如何将这两个框架结合使用,实现一个自定义表单登录的功能。自定义表单登录意味着我们可以根据应用需求设计登录界面,并且处理用户提交的登录信息。 1. ...

    springSecurityTest.zip

    这个"springSecurityTest.zip"文件是一个IDEA(IntelliJ IDEA)与MAVEN项目,设计用于帮助初学者理解并入门Spring Security。下面将详细阐述Spring Security的主要概念和其在实际开发中的应用。 首先,Spring ...

    Spring Security学习总结二

    自定义`UserDetailsService`,尤其是使用数据库作为数据源,是Spring Security中一项重要的技能。它不仅能够实现用户信息的动态管理和权限控制,还能提高应用程序的安全性和灵活性。通过理解`JdbcDaoImpl`的工作原理...

    Spring Security OAuth 笔记.doc

    Spring Security OAuth 是一个广泛使用的安全框架,它提供了OAuth 2.0的实现,用于构建安全的Web应用程序和服务。OAuth允许第三方应用以受限制的方式访问用户的数据,同时保持用户的隐私。以下是关于Spring Security...

Global site tag (gtag.js) - Google Analytics