HttpServletResponse#addHeader有什么问题?

时间:2012-09-23 15:14:54

标签: java servlets spring-security servlet-filters

我正在我的网络应用过滤器中运行,该过滤器会收到有关用户的外部来源信息,如果他已登录或未登录。继承人我的过滤器:

@Override
public void doFilter( ServletRequest request, ServletResponse response,
                      FilterChain chain ) throws IOException, ServletException
{
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpServletResponse httpResponse = (HttpServletResponse) response;

    String loginBean = httpRequest.getHeader( CommonVariables.LOGIN_BEAN );
    if ( loginBean == null )
    {
        System.out.println( "FILTER-----------" );
        try
        {
            String login;
            String domain;
            //Here i'm getting login and domain string
            loginBean = domain + "\\" + login;
            httpResponse.addHeader( "LoginBean", loginBean );
            System.out.println( login + " " + domain );
        } catch ( Exception e )
        {
            e.printStackTrace();
            //redirect to login page
            httpResponse.sendRedirect( "..." );
            return;
        }
    }
    chain.doFilter( request, response );
}

不是我虽然那些标题将被传递到下一个过滤器。因此我实现了Spring Security PRE_AUTH_FILTER:

Spring安全上下文

<http use-expressions="true" auto-config="false" entry-point-ref="http403EntryPoint">
    <!-- Additional http configuration omitted -->
    <custom-filter position="PRE_AUTH_FILTER" ref="siteminderFilter" />
</http>

<beans:bean id="siteminderFilter" class=
        "org.springframework.security.web.authentication.preauth.RequestHeaderAuthenticationFilter">
    <beans:property name="principalRequestHeader" value="LoginBean"/>
    <beans:property name="authenticationManager" ref="authenticationManager" />
</beans:bean>

<beans:bean id="preauthAuthProvider"
      class="org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider">
    <beans:property name="preAuthenticatedUserDetailsService">
        <beans:bean id="userDetailsServiceWrapper"
              class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">
            <beans:property name="userDetailsService" ref="userDetailsService"/>
        </beans:bean>
    </beans:property>
</beans:bean>

<beans:bean id="http403EntryPoint" class="org.springframework.security.web.authentication.Http403ForbiddenEntryPoint"/>
<beans:bean id="userDetailsService" class="com.execon.security.CustomUserDetailsService"/>

<authentication-manager alias="authenticationManager">
    <authentication-provider ref="preauthAuthProvider" />
</authentication-manager>

然后我尝试在CustoUserDetailsS​​ervice中解析loginBean String并接收actuall用户对象。但它没有被解雇,应用程序失败了:

org.springframework.security.web.authentication.preauth.PreAuthenticatedCredentialsNotFoundException: LoginBean header not found in request.

那么这意味着标题设置错误了吗?还是根本没有设置?什么可能是错的?

过滤器设置LoginBean首先被激活,然后进入Spring SEcurity。标准输出可以正常工作:

17:12:15,669 INFO  [stdout] (http--127.0.0.1-8080-2) FILTER-----------
17:12:15,669 INFO  [stdout] (http--127.0.0.1-8080-2) LOGIN DOMAIN

1 个答案:

答案 0 :(得分:2)

您正在设置response中的某些内容,而Spring的课程正在寻找request中的内容。

修改传入HttpServletRequest的唯一方法是装饰它。您应首先按如下方式定义一个类:

public class AuthHttpServletRequest extends HttpServletRequestWrapper
{
    private String loginBean;

    public AuthHttpServletRequest(HttpServletRequest aRequest, String loginBean)
    {
        super(aRequest);
        this.loginBean = loginBean;
    }

    @Override
    public String getHeader(String headerName)
    {
        if(CommonVariables.LOGIN_BEAN.equals(headerName)) {
            return this.loginBean;
        }
        return super.getHeader(headerName);
    }
}

然后,替换过滤器中的以下行:

httpResponse.addHeader( "LoginBean", loginBean );

用这个:

request = new AuthHttpServletequest(httpRequest, loginBean);

然后,您的chain.doFilter获取可以按照您的意图将loginBean返回到Spring的身份验证过滤器类的请求,位于过滤器链中。