我一直在使用Spring Security 3.x来处理我的项目的用户身份验证,到目前为止,它已经完美运行.
我最近收到了一个新项目的要求.在此项目中,它需要两组用户身份验证:一组用于根据LDAP对员工进行身份验证,另一组用于根据数据库对客户进行身份验证.我对如何在Spring Security中配置它感到有点困惑.
我最初的想法是创建一个具有以下字段的登录屏幕: -
j_username 用户字段.j_password 密码字段.如果用户选择"employee",那么我希望Spring Security针对LDAP对其进行身份验证,否则将根据数据库对凭据进行身份验证.但是,问题是表单将被提交,/j_spring_security_check并且我无法将单选按钮字段发送到我实现的自定义身份验证提供程序.我最初的想法是,我可能需要两个表单提交URL,而不是依赖于默认值/j_spring_security_check.每个URL都将由不同的身份验证提供程序处理,但我不确定如何在Spring Security中配置它.
我知道在Spring Security中,我可以配置后备身份验证,例如,如果LDAP身份验证失败,那么它将回退到数据库身份验证,但这不是我在这个新项目中拍摄的内容.
有人可以分享我应该如何在Spring Security 3.x中配置它?
谢谢.
更新 - 01-28-2011 - @ EasyAngel的技术
我正在尝试执行以下操作: -
/j_spring_security_check_for_employee/j_spring_security_check_for_customer我想要2种不同的表单登录的原因是允许我根据用户不同地处理身份验证,而不是进行后备身份验证.在我的情况下,员工和客户可能拥有相同的用户ID.
我合并了@ EasyAngel的想法,但必须更换一些已弃用的类.我目前面临的问题是没有过滤进程URLS似乎在Spring Security中注册,因为我不断获取Error 404: SRVE0190E: File not found: /j_spring_security_check_for_employee.我的直觉是springSecurityFilterChainbean没有正确连接,因此根本不使用我的自定义过滤器.
顺便说一下,我正在使用WebSphere,我确实com.ibm.ws.webcontainer.invokefilterscompatibility=true在服务器中设置了属性.我可以/j_spring_security_check毫无问题地达到默认值.
这是我完整的安全配置: -
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:sec="http://www.springframework.org/schema/security" 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.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
<sec:http auto-config="true">
<sec:form-login login-page="/login.jsp" authentication-failure-url="/login.jsp?login_error=1" default-target-url="/welcome.jsp"
always-use-default-target="true" />
<sec:logout …Run Code Online (Sandbox Code Playgroud) java authentication spring forms-authentication spring-security
我正在尝试使用Spring安全性Java配置开发Spring Boot Web应用程序并保护它.
按照Spring博客中的建议将静态Web资源放入' src/main/resources/public '后,我就能获得静态资源.即在浏览器中点击确实提供html内容.https://localhost/test.html
启用Spring Security后,点击静态资源URL需要进行身份验证.
我的相关Spring Security Java配置如下所示: -
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http.
authorizeRequests()
.antMatchers("/","/public/**", "/resources/**","/resources/public/**")
.permitAll()
.antMatchers("/google_oauth2_login").anonymous()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/")
.loginProcessingUrl("/login")
.defaultSuccessUrl("/home")
.and()
.csrf().disable()
.logout()
.logoutSuccessUrl("/")
.logoutUrl("/logout") // POST only
.and()
.requiresChannel()
.anyRequest().requiresSecure()
.and()
.addFilterAfter(oAuth2ClientContextFilter(),ExceptionTranslationFilter.class)
.addFilterAfter(googleOAuth2Filter(),OAuth2ClientContextFilter.class)
.userDetailsService(userService);
// @formatter:on
}
Run Code Online (Sandbox Code Playgroud)
我应该如何配置antMatchers以允许放置在src/main/resources/public中的静态资源?
我正在设置Spring Security来处理日志记录用户.我已经以用户身份登录,并在成功登录后被带到Access Denied错误页面.我不知道我的用户实际分配了什么角色,或者导致访问被拒绝的规则,因为我无法弄清楚如何为Spring Security库启用调试.
我的安全xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans ... >
<!-- security -->
<security:debug/><!-- doesn't seem to be working -->
<security:http auto-config="true">
<security:intercept-url pattern="/Admin**" access="hasRole('PROGRAMMER') or hasRole('ADMIN')"/>
<security:form-login login-page="/Load.do"
default-target-url="/Admin.do?m=loadAdminMain"
authentication-failure-url="/Load.do?error=true"
username-parameter="j_username"
password-parameter="j_password"
login-processing-url="/j_spring_security_check"/>
<security:csrf/><!-- enable Cross Site Request Forgery protection -->
</security:http>
<security:authentication-manager>
<security:authentication-provider>
<security:jdbc-user-service data-source-ref="loginDataSource"
users-by-username-query="SELECT username, password, active FROM userinformation WHERE username = ?"
authorities-by-username-query="
SELECT ui.username, r.rolename
FROM role r, userrole ur, userinformation ui
WHERE ui.username=?
AND ui.userinformationid = ur.userinformationid
AND ur.roleid = r.roleid " …Run Code Online (Sandbox Code Playgroud) Userprincipal是否从SecurityContextHolder绑定到请求或会话中检索?
UserPrincipal principal = (UserPrincipal) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
这是我访问当前登录用户的方式.如果当前会话被销毁,这会失效吗?
与之相反:如何使用spring security手动注销用户?
在我的应用程序中,我已经注册了新用户屏幕,该屏幕发布到控制器,该控制器在db中创建新用户(并进行一些明显的检查).然后我希望这个新用户自动登录...我有点想要一些东西像这样 :
SecurityContextHolder.getContext().setPrincipal(MyNewUser);
Run Code Online (Sandbox Code Playgroud)
编辑 好我几乎已经基于如何以Spring Security 3.1以编程方式登录用户的答案实现
Authentication auth = new UsernamePasswordAuthenticationToken(MyNewUser, null);
SecurityContextHolder.getContext().setPrincipal(MyNewUser);
Run Code Online (Sandbox Code Playgroud)
但是,在部署时,jsp无法访问我,MyNewUser.getWhateverMethods()而在正常登录过程之后也是如此.这个代码在名义上有效,但在登录时会抛出错误如下:
<sec:authentication property="principal.firstname" />
Run Code Online (Sandbox Code Playgroud) 我在Spring Boot的应用程序中添加了一个自定义安全配置,但是在LOG文件中仍然显示有关"使用默认安全密码"的消息.
有没有删除它?我不需要这个默认密码.看来Spring Boot没有认识到我的安全策略.
@Configuration
@EnableWebSecurity
public class CustomSecurityConfig extends WebSecurityConfigurerAdapter {
private final String uri = "/custom/*";
@Override
public void configure(final HttpSecurity http) throws Exception {
http.csrf().disable();
http.headers().httpStrictTransportSecurity().disable();
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
// Authorize sub-folders permissions
http.antMatcher(uri).authorizeRequests().anyRequest().permitAll();
}
}
Run Code Online (Sandbox Code Playgroud) 我定义了以下bean:
<sec:authentication-manager alias="authenticationManager">
<sec:authentication-provider
user-service-ref="userDetailsService" />
</sec:authentication-manager>
Run Code Online (Sandbox Code Playgroud)
我猜这里Spring使用了一些默认的实现AuthenticationManager.
在我的Java代码中,我有:
@Resource(name = "authenticationManager")
private AuthenticationManager authenticationManager; // specific for Spring Security
public boolean login(String username, String password) {
try {
Authentication authenticate = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
if (authenticate.isAuthenticated()) {
SecurityContextHolder.getContext().setAuthentication(authenticate);
return true;
}
}
catch (AuthenticationException e) {
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
这里AuthenticationManager.authenticate(...)叫.但是我想知道AuthenticationManagerSpring默认使用哪种实现,以及它authenticate(...)为了进行身份验证(即确保用户名与密码匹配)的作用.
你能解释一下吗?
有人能告诉我一个AuthenticationManager和一个AuthenticationProviderSpring Security 之间的区别吗?
它们是如何使用的以及如何调用它们.我的理解是,一个SecurityFilter会调用对AuthenticationManager一个Authentication对象进行身份验证吗?但那么它在AuthenticationProvider哪里发挥作用?
谢谢!
我在Spring Security配置中配置了两个身份验证提供程序:
<security:authentication-manager>
<security:authentication-provider ref="XProvider" />
<security:authentication-provider ref="YProvider" />
</security:authentication-manager>
Run Code Online (Sandbox Code Playgroud)
Spring安全性是否评估两个提供商?或者它是否停止评估其中一个是否失败?如果没有,如何让它停止?
谢谢.
从Spring Security 3.1.4.RELEASE开始,旧的org.springframework.security.authentication.encoding.PasswordEncoder 已经被弃用了org.springframework.security.crypto.password.PasswordEncoder.由于我的应用尚未向公众发布,我决定转向新的,而不是弃用的API.
到目前为止,我有一个ReflectionSaltSource自动使用用户的注册日期作为每用户盐密码.
String encodedPassword = passwordEncoder.encodePassword(rawPassword, saltSource.getSalt(user));
Run Code Online (Sandbox Code Playgroud)
在登录过程中,Spring还使用我的bean来验证用户是否可以登录.我无法在新密码编码器中实现这一点,因为SHA-1的默认实现 - StandardPasswordEncoder只能添加全局编码器创建过程中的秘密盐.
有没有合理的方法如何使用未弃用的API进行设置?