java 春季安全。自定义身份验证管理器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15304718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring security. Custom AuthenticationManager
提问by Igor Masternoy
I have spring mvc application. And i've added Spring Security with CustomAuthenticationManager. This is application-security.xml that included in appContex.xml. In login.jsp i use standart login page with 2 inputs: name='j_username' & name='j_password' and than in method in CustomAuthenticationManager.authenticate(Authentication authentication) i receive only password, but not principal with it name. What am i do wrong?
我有 spring mvc 应用程序。我已经使用 CustomAuthenticationManager 添加了 Spring Security。这是包含在 appContex.xml 中的 application-security.xml。在 login.jsp 中,我使用带有 2 个输入的标准登录页面:name='j_username' & name='j_password' 而在 CustomAuthenticationManager.authenticate(Authentication authentication) 中的方法中,我只收到密码,但不收到带有它的名称的主体。我做错了什么?
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p" xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">
<bean id="loginPassAuthenticationFilter"
class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
<property name="authenticationManager" ref="customAuthenticationManager" />
<property name="allowSessionCreation" value="true" />
<property name="filterProcessesUrl" value="/j_spring_security_check" />
<property name="authenticationFailureHandler" ref="customAuthenticationFailureHandler" />
<property name="authenticationSuccessHandler" ref="customAuthenticationSuccessHandler" />
</bean>
<bean id="customAuthenticationManager"
class="com.whatever.security.CustomAuthenticationManager" />
<bean id="authenticationFilter"
class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter"
p:authenticationManager-ref="customAuthenticationManager"
p:authenticationFailureHandler-ref="customAuthenticationFailureHandler"
p:authenticationSuccessHandler-ref="customAuthenticationSuccessHandler" />
<bean id="customAuthenticationFailureHandler"
class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler"
p:defaultFailureUrl="/?error=true" />
<bean id="customAuthenticationSuccessHandler"
class="org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler"
p:defaultTargetUrl="/" />
<bean id="authenticationEntryPoint"
class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint"
p:loginFormUrl="/" />
<security:authentication-manager />
<bean id="passwordEncoder"
class="org.springframework.security.authentication.encoding.ShaPasswordEncoder">
<constructor-arg value="512" />
</bean>
<form name='f' action="<c:url value='j_spring_security_check' />"
method='POST'>
<table>
<tr>
<td>User:</td>
<td><input type='text' name='j_username' value=''></td>
</tr>
<tr>
<td>Password:</td>
<td><input type='password' name='j_password' /></td>
</tr>
<tr>
<td colspan='2'><input name="submit" type="submit"
value="submit" /></td>
</tr>
<tr>
<td colspan='2'><input name="reset" type="reset" /></td>
</tr>
</table>
</form>
回答by Michael
I recommend to use Security Namespace (link to the example of minimal configuration):
http://static.springsource.org/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#ns-minimal
我建议使用安全命名空间(链接到最小配置示例):http:
//static.springsource.org/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#ns-minimal
Here the link how you can define your own authentication provider : http://static.springsource.org/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#ns-auth-manager
这里是如何定义自己的身份验证提供程序的链接:http: //static.springsource.org/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#ns-auth-manager
Creation of your own authentication provider solves most use cases. Please tell me if you need any additional clarifications.
创建您自己的身份验证提供程序可以解决大多数用例。如果您需要任何其他说明,请告诉我。
Best regards,
最好的祝福,
Michael
迈克尔