Spring Security入门

参考官方文档(http://www.mossle.com/docs/springsecurity3/html/technical-overview.html)

spring 是一个非常流行和成功的 Java 应用开发框架。Spring Security 基于 Spring 框架,提供了一套 Web 应用安全性的完整解决方案。一般来说,Web 应用的安全性包括用户认证(Authentication)和用户授权(Authorization)两个部分。

web.xml配置
<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>

Once you've added this to your web.xml, you're ready to start editing your application context file. Web security services are configured using the <http> element.

spring http 配置
 <http auto-config='true'>
    <intercept-url pattern="/**" access="permitAll" />
    <custom-filter position="PRE_AUTH_FILTER" ref="dtSessionMgr"/> 
  </http>

<beans:bean id="dtSessionMgr" class="com.duitang.common.DuitangSessionMgr" p:authenticationManager-ref="authenticationManager"/>
 

使用 Authentication Providers

定义authenticationManager,dtAuthProvider是某个实现bean。
<authentication-manager alias="authenticationManager">
        <authentication-provider ref="dtAuthProvider"/>
    </authentication-manager>


配置具体的实现bean:
<beans:bean id="dtAuthProvider"
        class="org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider">
        <beans:property name="preAuthenticatedUserDetailsService">
            <beans:bean class="com.duitang.common.authen.DuitangAuthProvider"/>
        </beans:property>
    </beans:bean>


PreAuthenticatedAuthenticationProvider是一个具体的实现,接口定义在org.springframework.security.authentication.AuthenticationProvider
AuthenticationProvider有多个实现,后面一一介绍。


LDAP
对LDAP(轻量目录访问协议)的支持,org.springframework.security.ldap.authentication.
LdapAuthenticationProvider。
参考: http://www.ibm.com/developerworks/cn/java/j-lo-springsecurity/index.html


获取登录用户信息
org.springframework.security.core.context.SecurityContext接口表示的是当前应用的安全上下文。通过此接口可以获取和设置当前的认证对象。

org.springframework.security.core.Authentication接口用来表示此认证对象。通过认证对象的方法可以判断当前用户是否已经通过认证,以及获取当前认证用户的相关信息,包括用户名、密码和权限等。

protected DuitangUser parseDuitangUser() {
	Authentication  auth1 = SecurityContextHolder.getContext()
	.getAuthentication();

	Object userobj = auth1.getPrincipal();

	if (userobj == null) {
		return null;
	}
	if (userobj.getClass() != DuitangUser.class) {
		return null;
	}
	return (DuitangUser) userobj;
}


参考spring官方文档:http://static.springsource.org/spring-security/site/docs/3.0.x/reference/springsecurity-single.html#get-spring-security

http://cosmo2097.iteye.com/blog/349003

你可能感兴趣的:(Spring Security)