【发布时间】:2014-05-23 16:50:08
【问题描述】:
我正在实现 MyUsernamePasswordAuthenticationFilter 扩展默认过滤器。
我什么都没做。我只是想在此处获取请求以使其变得简单。
public class MyUsernamePasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
return super.attemptAuthentication(request, response);
}
}
我在请求中传递了我的用户名和密码参数。
j_username=Test&j_password=Test
我正在身份验证提供程序中进行身份验证
public class MyiAuthenticationProvider
implements AuthenticationProvider
{
public Authentication authenticate(Authentication a)
throws AuthenticationException
{
UsernamePasswordAuthenticationToken auth = (UsernamePasswordAuthenticationToken) a;
System.out.println("The user name is"+ String.valueOf(auth.getPrincipal()));
----
----
}
}
我的应用上下文是这样的
<sec:http auto-config="false" entry-point-ref="MyAuthenticationEntryPoint"
create-session="always">
<sec:custom-filter position="FORM_LOGIN_FILTER"
ref="myUsernamePasswordAuthenticationFilter" />
<sec:logout logout-url="/logout" success-handler-ref="MyLogoutSuccessHandler" />
</sec:http>
<bean id="myUsernamePasswordAuthenticationFilter"
class="my.test.site.security.MyUsernamePasswordAuthenticationFilter">
<property name="filterProcessesUrl" value="/login" />
<property name="authenticationManager" ref="authenticationManager" />
<property name="authenticationFailureHandler" ref="myAuthenticationFailureHandler" />
<property name="authenticationSuccessHandler" ref="myLoginSuccessHandler" />
</bean>
<sec:authentication-manager alias="authenticationManager">
<sec:authentication-provider ref="myAuthenticationProvider" />
</sec:authentication-manager>
<bean id="myAuthenticationProvider" class="my.test.site.security.MyAuthenticationProvider" />
<bean id="myAuthenticationEntryPoint" class="my.test.site.security.MyAuthenticationEntryPoint" />
<bean id="myLoginSuccessHandler" class="my.test.site.security.MyLoginSuccessHandler" />
<bean id="myLogoutSuccessHandler" class="my.test.site.security.MyLogoutSuccessHandler" />
<bean id="myAuthenticationFailureHandler" class="my.test.site.security.MyAuthenticationFailureHandler" />
<bean id="myPreAuthFilter" class="my.test.site.security.MyPreAuthenticationFilter" />
我得到空的用户名和密码。
我是否缺少任何配置?
【问题讨论】:
-
这行
System.out.println("The user name is"+ String.valueOf(auth.getPrincipal()));可能是问题所在。 -
@NandkumarTekale。感谢您的回复。它仅供参考。我得到空身份验证对象。
-
你能用登录表单的jsp部分更新你的问题吗?
-
@DebojitSaikia。我正在使用 html 表单登录。我得到了正确的请求,但无法弄清楚为什么我的 Authentication 对象为空。如果您能建议配置中是否缺少任何东西,那就太好了。
-
你能把那个表单部分贴出来吗:
<form>...</form>
标签: java spring spring-security