正如标题所说,我正在开发一个从外部应用程序接收用户身份验证信息的 Web 应用程序。我的应用程序的 Spring Controller 获取用户信息并将其存储在 session 中。我想在 Spring Security 中对这个用户进行身份验证,然后使用他的角色来授予/拒绝对 url 的访问,例如
<intercept-url pattern="/myprotectedpage*" access="hasRole('rightrole')" />
我阅读了一些关于 PRE_AUTH_FILTER 和 UserDetailsService 的教程,但我不明白这一点。 Spring Security 的应用生命周期是怎样的?涉及哪些类(class)?
我需要一些完整的工作样本。
最佳答案
有很多相同的东西,只需要正确地谷歌。
无论如何,迄今为止我发现的最好的(几乎所有的 Spring 类(class))是 Krams这是基本的 Spring 安全性。
http://krams915.blogspot.com/2010/12/spring-security-mvc-integration_18.html
实现 UserDetailService 这里是链接
http://krams915.blogspot.in/2012/01/spring-security-31-implement_5023.html
其他一些是:
编辑
这就是我自己的应用程序进行身份验证的方式(请注意,我不使用外部身份验证,我只是从 DB 获取详细信息,但我想这应该不是什么大问题)。
我的
security-context.xml
:<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">
<global-method-security pre-post-annotations="enabled" jsr250-annotations="enabled" secured-annotations="enabled">
</global-method-security>
<http use-expressions="true">
<intercept-url pattern="/favicon.ico" access="permitAll" />
<intercept-url pattern="/static/**" access="permitAll"/>
<intercept-url pattern="/login.jsp*" access="permitAll"/>
<intercept-url pattern="/Admin/**" access="hasAnyRole('ROLE_SUPER_USER')"/>
<intercept-url pattern="/**" access="hasAnyRole('ROLE_USER','ROLE_SUPER_USER','ROLE_ADMIN'"/>
<form-login login-page="/login.jsp" authentication-failure-url="/login.jsp?login_error=1" />
<http-basic/>
<logout logout-success-url="/login.jsp"/>
<remember-me user-service-ref="loginService" /
</http>
<authentication-manager>
<authentication-provider user-service-ref="loginService">
<password-encoder hash="md5"/>
</authentication-provider>
</authentication-manager>
<beans:bean id="loginService" class="com.indyaah.service.LoginService">
</beans:bean>
<beans:bean id="authService" class="com.indyaah.service.AuthService" />
</beans:beans>
现在如您所见,我指定了一个名为
loginService
的 bean作为我的身份验证提供程序,它是类 com.indyaah.service.LoginService
的 bean .相同的代码是:
Pl 注意我截断了不必要的代码
package com.indyaah.service;
..
@Service
public class LoginService implements UserDetailsService {
....
/**
* Implementation for custom spring security UserDetailsService
*/
public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException, DataAccessException {
logger.debug("Inside get member by username");
if (userName != null) {
Member memberVO = memberMapper.getMemberByUsername(userName);
if (memberVO != null) {
ArrayList<String> authList = memberRolesMapper.getMemberRoles(memberVO.getMemberId());
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
for (String role : authList) {
System.out.println(role);
authorities.add(new GrantedAuthorityImpl(role.toString()));
}
if (memberVO.getEnabled()) {
User user = new User(memberVO.getUserName(), memberVO.getPassword(), true, true, true, true, authorities);
return user;
} else {
logger.error("User with login: " + userName + " not Enabled in database. Authentication failed for user ");
throw new UsernameNotFoundException("User Not Enabled");
}
} else {
logger.error("User with login: " + userName + " not found in database. Authentication failed for user ");
throw new UsernameNotFoundException("user not found in database");
}
} else {
logger.error("No User specified in the login ");
throw new UsernameNotFoundException("No username specified");
}
}
}
请注意这里的 2 件事。
org.springframework.security.core.userdetails.User
下。对象然后由方法返回到 Spring 安全性。 关于Spring 安全仅用于授权。外部认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12366763/