Java 获取空的或空的SecurityContextHolder.getContext().getAuthentication()

Java 获取空的或空的SecurityContextHolder.getContext().getAuthentication(),java,spring,spring-security,Java,Spring,Spring Security,我正在实现MyUsernamePasswordAuthenticationFilter扩展默认筛选器 我什么也没做。我只是想让请求变得简单 public class MyUsernamePasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter { @Override public Authentication attemptAuthentication(HttpServletRequest

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

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


我是否缺少任何配置?

事实上,您没有做任何事情,因为您正在从请求读取输入流

如果您检查Javadoc中的
HttpServletRequest
类,则会出现以下情况


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

此行
System.out.println(“用户名是”+String.valueOf(auth.getPrincipal())可能是问题所在。@NandkumarTekale。谢谢你的回复。仅供参考。我得到了空的身份验证对象。你能用jsp的一部分更新你的问题吗?有登录表单吗?@DebojitSaikia。我正在使用html表单登录。我正在正确获取请求,但无法找出身份验证对象为null的原因。如果您能建议配置中是否缺少任何内容,那就太好了。您可以发布该表单部分:
谢谢您的回复。我刚刚检查了SecurityContextHolder.getContext().getAuthentication();是空的。如果你能帮我解决这个问题就太好了。即使删除了代码,我仍然会得到相同的空值。相应地对问题进行了编辑。抱歉,您不断更改您的问题,并且不清楚您是否正确理解了所问问题的基本内容。由您的
AuthenticationProvider
提供身份验证对象,因此这也是您的代码。另外,看起来您还没有完全按照原样输入配置(bean引用“MyAuthenticationProvider”以大写字母开头,因此它根本不应该运行)。使用调试器并检查调试日志以了解发生了什么。