【问题标题】:Securing Web Application with Spring Boot is not working使用 Spring Boot 保护 Web 应用程序不起作用
【发布时间】:2020-08-08 06:56:45
【问题描述】:

我已经实现了官方春季指南中的一个示例: Securing a Web Application

我放了三个html文件/resources/templates:

HELLO.HTML

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"
      xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <head>
        <title>Hello World!</title>
    </head>
    <body>
        <h1 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h1>
        <form th:action="@{/logout}" method="post">
            <input type="submit" value="Sign Out"/>
        </form>
    </body>
</html>

主页 HTML

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org" xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <head>
        <title>Spring Security Example</title>
    </head>
    <body>
        <h1>Welcome!</h1>

        <p>Click <a th:href="@{/hello}">here</a> to see a greeting.</p>
    </body>
</html>

登录 HTML

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"
      xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <head>
        <title>Spring Security Example </title>
    </head>
    <body>
        <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">
            <div><label> User Name : <input type="text" name="username"/> </label></div>
            <div><label> Password: <input type="password" name="password"/> </label></div>
            <div><input type="submit" value="Sign In"/></div>
        </form>
    </body>
</html>

MVC 配置是

@Configuration

public class MVCConfig {

    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/home").setViewName("home");
        registry.addViewController("/").setViewName("home");
        registry.addViewController("/hello").setViewName("hello");
        registry.addViewController("/login").setViewName("login");

    }
}

安全配置类:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/", "/home").permitAll().anyRequest().authenticated().and().formLogin()
                .loginPage("/login").permitAll().and().logout().permitAll();
    }

    @Bean
    @Override
    public UserDetailsService userDetailsService() {
        UserDetails user = User.withDefaultPasswordEncoder().username("user").password("password").roles("USER")
                .build();

        return new InMemoryUserDetailsManager(user);
    }
}

当我运行应用程序并打开 127.0.0.1:8080/ 链接时,我遇到了错误:

There was an unexpected error (type=Not Found, status=404).
No message available

我从文件中删除了 thymeleaf 标签,并使文件成为静态文件。然后我把它们移到/static。 登录表单如下:

<form "/login" method="post">
            <div><label> User Name : <input type="text" name="username"/> </label></div>
            <div><label> Password: <input type="password" name="password"/> </label></div>
            <div><input type="submit" value="Sign In"/></div>
        </form>

并更改了 SecurityConfig:

http.authorizeRequests().antMatchers("/", "/home").permitAll().anyRequest().authenticated().and().formLogin()
                .loginPage("**/login.html**").permitAll().and().logout().permitAll();

现在我可以打开位置 127.0.0.1:8080/login.html,但提交表单后没有进行身份验证。 (显示 403 错误)

我该怎么办?

【问题讨论】:

    标签: spring-boot spring-mvc spring-security


    【解决方案1】:

    这是一个老问题,您应该已经解决了。无论如何,如果其他人有这个问题,我也是春天的新手,在本教程中遇到了和你一样的问题。所以我尝试解决百里香问题,一切都开始正常工作,包括身份验证。

    我所做的只是在我的 pom.xml 中包含以下依赖项

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
    

    【讨论】:

      猜你喜欢
      • 2018-02-22
      • 2015-11-30
      • 2017-08-09
      • 2015-05-12
      • 2020-01-08
      • 2014-10-19
      • 2017-11-11
      • 2018-07-31
      • 2020-11-29
      相关资源
      最近更新 更多