【问题标题】:Spring Security + Container Managed Authentication issueSpring Security + Container Managed Authentication 问题
【发布时间】:2014-07-04 06:30:29
【问题描述】:

我正在使用 Spring Security Framework + Spring MVC 来构建安全的 Web 应用程序。我的用户的身份验证必须由我的容器处理。为了实现这一点,我遵循了这两个步骤:

  1. 在我的 web.xml 文件中保护我的整个应用程序:

    <login-config>
        <auth-method>FORM</auth-method>
    </login-config>
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Public</web-resource-name>
            <description>Matches unconstrained pages</description>
            <url-pattern>/resources/*</url-pattern>
        </web-resource-collection>
    </security-constraint>
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Secured Areas</web-resource-name>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>Everyone</role-name>
        </auth-constraint>
    </security-constraint>
    

  2. 使用以下类设置 Spring Security:

    @Configuration
    @EnableWebMvcSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/resources/**").permitAll()
                .anyRequest().authenticated()
            .and()
                .jee().mappableRoles("Admin", "User");
    }
    

    }

此配置似乎有效,因为每当我尝试访问应用程序上下文中的任何 URL(静态资源(在 /resources/ 文件夹中)除外)时,我都必须进行身份验证。然后登录后,会显示正确的 URL 和页面(如预期的那样)。但是,如果我尝试访问另一个链接,我必须重新登录。然后,显示正确的 URL 和页面,我可以访问任何链接而无需再次登录。所以,我的问题是:为什么我必须登录两次才不会被要求再次登录?

【问题讨论】:

    标签: java spring jakarta-ee spring-security


    【解决方案1】:

    我并不是这个主题的真正专家,但我认为会话可能是由容器和 Spring Security 创建的。我的容器身份验证由 WebLogic 处理。有一堆谷歌结果展示了如何为 preauth 编写自定义过滤器,但它似乎仍然无法正常工作,直到我将 spring security 更改为无状态。

    <http auto-config="false" use-expressions="true" disable-url-rewriting="true"
        create-session="stateless" entry-point-ref="http403EntryPoint">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-31
      • 2015-12-04
      • 1970-01-01
      • 2023-02-10
      • 2017-11-21
      • 2021-11-12
      相关资源
      最近更新 更多