【发布时间】:2014-07-04 06:30:29
【问题描述】:
我正在使用 Spring Security Framework + Spring MVC 来构建安全的 Web 应用程序。我的用户的身份验证必须由我的容器处理。为了实现这一点,我遵循了这两个步骤:
-
在我的 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> -
使用以下类设置 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