【问题标题】:Spring boot security invalid requestURISpring Boot 安全性无效 requestURI
【发布时间】:2015-03-23 18:26:15
【问题描述】:

尝试使用 Spring boot 和 thymeleaf 创建一个简单的登录页面。但是由于某种原因,当用户在成功输入详细信息后尝试登录时,会将他们带到无效页面,即 javascript 页面

requestURI: arg1=/js/jquery-1.11.2.min.js; arg2=/login (property not equals)

例如,用户点击 /cms/home 并被带到登录页面,一旦他们登录,他们应该被带回 /cms/home。这是我的代码

@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
public void configure(HttpSecurity httpSecurity) throws Exception{

    httpSecurity
            .authorizeRequests()
            .antMatchers("/", "/resources/**").permitAll()
            .anyRequest().fullyAuthenticated()
            .and()
            .formLogin()
            .loginPage("/login")
            .failureUrl("/login?error")
            .permitAll()
            .and()
            .logout()
            .logoutUrl("/logout")
            .logoutSuccessUrl("/")
            .permitAll();


}

@Override
public void configure(WebSecurity security){
    security.ignoring().antMatchers("/css/**","/fonts/**","js/**","/CMS/css/**","/CMS/js/**","/libs/**");
}

这里是登录页面

   <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8"/>

    <link href="../static/css/bootstrap.min.css"
          th:href="@{/css/bootstrap.min.css}"
          rel="stylesheet" media="screen" />

    <script href="../static/js/bootstrap.min.js"
            th:href="@{/js/bootstrap.min.js}"
            ></script>

    <script  href="../static/js/jquery-1.11.2.min.js"
            th:src="@{/js/jquery-1.11.2.min.js}"></script>

    <link href="../static/CMS/css/login.css"
          th:href="@{/CMS/css/login.css}"
          rel="stylesheet" media="screen" />



    <title>CMS Login</title>
</head>
<body>

<div class="container">

    <div class="row" id="pwd-container">
        <div class="col-md-4"></div>

        <div class="col-md-4">
            <section class="login-form">
                <div th:if="${param.error}">
                    Invalid username and password.
                </div>
                <div th:if="${param.logout}">
                    You have been logged out.
                </div>
                <form  th:action="@{/login}"  method="post" role="login">



                    <input type="text" name="username" placeholder="Email" required="" class="form-control input-lg" value="myOwnUser" />

                    <input type="password"  name="password" class="form-control input-lg" id="password" placeholder="Password" required=""  value="myOwnPassword"/>

                    <button type="submit" name="go" class="btn btn-lg btn-primary btn-block">Sign in</button>
                    <div>
                       <a href="#">reset password</a>
                    </div>

                </form>

                <div class="form-links">
                    <a href="#">www.website.com</a>
                </div>
            </section>
        </div>

        <div class="col-md-4"></div>


    </div>
</div>

</body>
</html>

如果有人能帮忙就太好了

【问题讨论】:

    标签: java spring-mvc spring-boot thymeleaf


    【解决方案1】:

    在您的 ant 匹配器中,您忘记了 "js/**" 中的 /,因此在用户登录之前它不允许加载 /js/jquery-1.11.2.min.js,然后在登录后您的应用程序加载文件后的第一件事现在已获得授权。

    【讨论】:

    • 我还看到了静态文件夹中的所有内容。所以你可以把它改成一个被忽略的语句 security.ignoring().antMatchers("/static/**");
    • 由于某种原因,仅添加静态不起作用。我添加了缺少的斜杠,它现在可以按预期工作了,谢谢。
    • 添加“/static/**”是行不通的,因为 Spring Boot 会查看静态文件夹中的内容,因此只有在您具有 /static/static/ 结构时才会起作用。
    猜你喜欢
    • 1970-01-01
    • 2021-07-14
    • 2016-05-14
    • 2017-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-18
    相关资源
    最近更新 更多