【问题标题】:Configure the Domain attributes of the cookie header withing the spring or spring security在spring或spring security中配置cookie头的Domain属性
【发布时间】:2014-12-04 17:14:34
【问题描述】:

如何使用 spring security java config 配置 cookie 的 Domain 属性。 我需要将 cookie 的访问权限限制在一个特定的子域中,例如

Domain=.test.example.com;

现在我知道有一个类似于下面示例的 xml 配置,但是我不再在我的应用程序中添加任何 web.xml,我希望我的所有配置都通过 java 进行。

<session-config>
    <session-timeout>400</session-timeout>
    <cookie-config>
        <name>KSESSION</name>
        <path>/</path>
        <http-only>true</http-only>
        <secure>true</secure>
    </cookie-config>
</session-config>

在扩展 WebSecurityConfigurerAdapter 的 SecurityConfig 类中,我正在搜索一些会话配置对象或参数,但我找不到,即我只有 sessionManagement 对象。

    .sessionManagement()
        .enableSessionUrlRewriting(false)

        .sessionAuthenticationStrategy(sessionControlStrategy())
        .sessionCreationPolicy(SessionCreationPolicy.ALWAYS)
        .sessionFixation().newSession()

        .maximumSessions(1)
        .maxSessionsPreventsLogin(true)
        .expiredUrl("/login?expired")

也许这是通过 TomcatContextCustomizer bean 完成的,即我在那里找到了参数 setUseHttpOnly 和路径参数,但在域属性上没有设置器。

@Bean 
public TomcatContextCustomizer tomcatContextCustomizer() {
    System.out.println("TOMCATCONTEXTCUSTOMIZER INITILIZED");
    return new TomcatContextCustomizer() {

    @Override
    public void customize(Context context) {
        // TODO Auto-generated method stub
        context.addServletContainerInitializer(new WsSci(), null);
        context.setUseHttpOnly(true);
        context.setPath("/testBlaBlaPage");



    }
};

}

所以基本上我的目标是有一个这样的 http 标头

Set-Cookie: JSESSIONCookie: JSESSIONID=DEAC4422AB4E28A7062C08724C8BCFAA; Path=/login; Secure; Domain=.test.example.com; HttpOnly.

目前看起来像

Set-Cookie: JSESSIONCookie: JSESSIONID=DEAC4422AB4E28A7062C08724C8BCFAA; Path=/; Secure; HttpOnly

【问题讨论】:

    标签: spring spring-security spring-java-config


    【解决方案1】:

    好的,我找到了我的问题的答案,即它是 tomcat 上下文下的 setSessionCookieDomain() 方法。即类似

    context.setSessionCookieDomain(".test.example.com");

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      • 2012-01-23
      • 2011-01-08
      • 2014-12-07
      相关资源
      最近更新 更多