且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

不允许使用 Spring Security j_spring_security_check 的 Spring Boot

更新时间:2022-05-01 18:08:37

从下面的链接中,您可以看到注解 java config 的内容如下

From the below link, you can see that for annotation java config the following things hold

http://spring.io/blog/2013/07/03/spring-security-java-config-preview-web-security/

  1. GET/login 呈现登录页面而不是/spring_security_login

POST/login 验证用户而不是/j_spring_security_check

您需要进行以下更改才能使您的安全工作.

You need to make the following changes to get your security working.

按如下方式更改您的 spring 安全配置

    @Override
    protected void configure(HttpSecurity http) throws Exception {  http
        .authorizeRequests()
            .antMatchers("/login").permitAll()
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login/")
            .loginProcessingUrl("/login")
            .failureUrl("/login?error")
            .permitAll();
    }

您的 JSP 应该是(j_spring_security_check 替换为登录名,j_username 替换为用户名)

<form class="form-signin"name="f" action="${pageContext.request.contextPath}/login" method="POST">
            <fieldset>
                    <input class="form-control form-group" type="text" name="username" placeholder="Username">
                    <input class="form-control" type="password" name="password" placeholder="Password" >
                    <a class="forgot pull-right" href="#">Forgot password?</a>
                    <button name="submit" class="btn btn-block btn-primary" type="submit">Sign in</button>
            </fieldset>
        </form>

要将仪表板指定为默认目标 URL,您可以执行以下操作.

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/login").setViewName("login");
    registry.addViewController("/").setViewName("dashboard");
    registry.addViewController("/dashboard").setViewName("dashboard");
}