gpt4 book ai didi

java - 获取空或 null SecurityContextHolder.getContext().getAuthentication()

转载 作者:行者123 更新时间:2023-12-01 13:49:12 29 4
gpt4 key购买 nike

我正在实现 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" />

我得到的用户名和密码为空。

我是否缺少任何配置?

最佳答案

实际上您并没有执行任何操作,因为您正在从请求中读取输入流。

如果您检查 HttpServletRequest 类的 Javadoc,则此 "can interfere with the execution" of the getParameter method .

正常身份验证依赖于 getParameter 的使用,因此如果您希望流正常工作,则不应触摸该流。处理请求时,您应该自己读取请求,或者允许 servlet 容器执行此操作并使用 getParameter 方法访问参数。

关于java - 获取空或 null SecurityContextHolder.getContext().getAuthentication(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20095443/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com