【问题标题】:Spring Security /j_spring_security_check Not Found 404Spring Security /j_spring_security_check 未找到 404
【发布时间】:2015-02-12 17:02:41
【问题描述】:

这是我的 WebAppInitializer:

@Configuration
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] { AppConfig.class, WebSecurityConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { WebConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }
}

这是我的安全配置:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/resources/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        http.authorizeRequests()
                .antMatchers("/signup", "/about").permitAll()
                .antMatchers("/admin/**").hasRole("ADMIN")
                .anyRequest().authenticated()
                .and().formLogin().loginPage("/login").defaultSuccessUrl("/home").failureUrl("/error").permitAll()
                .and().httpBasic();
        // @formatter:on
    }

}

这是我的 login.html(来自 thymeleaf 的示例 html):

<form th:action="@{/j_spring_security_check}" method="post">
  <label for="j_username">Username</label>:
  <input type="text" id="j_username" name="j_username" /> <br />

  <label for="j_password">Password</label>:
  <input type="password" id="j_password" name="j_password" /> <br />

  <input type="submit" value="Log in" />
</form>

当我点击登录时,出现此错误:

HTTP ERROR 404

Problem accessing /j_spring_security_check. Reason:

    Not Found

我怎样才能摆脱这个错误?我google了很多,但还没有成功。 (是的,我不使用任何 xml。)

【问题讨论】:

    标签: spring spring-mvc spring-security thymeleaf


    【解决方案1】:

    我已经创建了这个,它现在可以工作了:

    import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
    
    public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
    
    }
    

    这显然会激活 SpringSecurityFilterChain。

    由于 CSRF 错误,我还不得不从 @EnableWebSecurity 切换到 @EnableWebMvcSecurity。如spring doc中所写:

    ...原因是 Spring Security 正在防止 CSRF attakcks 并且我们的请求中没有包含 CSRF 令牌。更新我们的 配置使用 @EnableWebMvcSecurity 注释,这将 与@EnableWebMvcSecurity 执行相同的操作并提供与 春天MVC。除其他外,它将确保我们的 CSRF 令牌 使用 Thymleaf 2.1+ 或 Spring 时自动包含在我们的表单中 MVC 标签库...

    也感谢 M. Deinum 的评论。 Spring 最近显然已将 /j_spring_security_check /j_password /j_username 切换为 /login /password /username。

    【讨论】:

    • 上述的替代方案是以编程方式初始化过滤器。 IE: FilterRegistration.Dynamic springSecurityFilterChain = container.addFilter("springSecurityFilterChain", DelegatingFilterProxy.class); springSecurityFilterChain.addMappingForUrlPatterns(null, false, "/*");对我来说,这种方法更有意义,或者您可以将其添加到您的应用程序 xml 中。做同样的事情,有一个空类似乎是错误的,如果由于不存在引用而没有评论,将来可能会被删除。
    【解决方案2】:

    如果您使用的是 spring 4.X 则可能是版本问题

    a.) 登录网址 = /login b.) 注销 url = /logout

    如果是 spring 3.X

    a.) 登录网址 = /j_spring_security_check b.) 注销 url = /j_spring_security_logout

    【讨论】:

      【解决方案3】:

      我在禁用 csrf 时也遇到了这个问题。我尝试明确指定 loginProcessingUrl 并且效果很好!

      @Override
      protected void configure(HttpSecurity httpSecurity) throws Exception {
         httpSecurity.csrf().disable();
         httpSecurity.formLogin().loginProcessingUrl("/login-url");
      }
      

      请注意:

      1. 这个网址只允许HTTP POST方法。
      2. Spring Security中的其他默认重要url有: 1./login:返回默认登录表单 2./logout

      我的Spring Security 版本是4.0.3.RELEASE

      P/S:我的回答可能与问题没有直接关系,但我认为它可以帮助像我一样不熟悉Spring Security 的人

      【讨论】:

        猜你喜欢
        • 2013-05-03
        • 2020-09-22
        • 2013-03-10
        • 2013-10-14
        • 2011-11-26
        • 2015-07-10
        • 2014-01-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多