【问题标题】:Getting empty or null SecurityContextHolder.getContext().getAuthentication()获取空或 null SecurityContextHolder.getContext().getAuthentication()
【发布时间】: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 对象为空。如果您能建议配置中是否缺少任何东西,那就太好了。
  • 你能把那个表单部分贴出来吗:&lt;form&gt;...&lt;/form&gt;

标签: java spring spring-security


【解决方案1】:

实际上你没有做任何事情并不是真的,因为你正在从请求中读取输入流。

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

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

【讨论】:

  • 感谢您的回复。我刚刚检查了 SecurityContextHolder.getContext().getAuthentication();一片空白。如果您能帮我解决这个问题,那就太好了。
  • 即使删除代码后,我仍然得到相同的空值。相应地编辑了问题。
  • 抱歉,您一直在更改您的问题,并且不清楚您是否从所问的内容中正确理解了基础知识。由您的AuthenticationProvider 提供身份验证对象,所以这又是您的代码。此外,您似乎还没有完全按原样输入配置(bean 引用“MyAuthenticationProvider”以大写字母开头,因此它根本不应该运行)。使用调试器并检查调试日志以了解发生了什么。
猜你喜欢
  • 2014-04-07
  • 2013-07-08
  • 2015-08-17
  • 1970-01-01
  • 2019-04-28
  • 2019-06-29
  • 2018-01-27
  • 1970-01-01
相关资源
最近更新 更多