[Java][Spring Boot] 使用NetBeans创建登录页面 – 开始Spring Boot(3)

我曾考虑放弃Spring MVC而选择使用Jersey(JAX-RS),但考虑到其文档整理的状况似乎非常困难,所以我决定还是继续使用Spring MVC。

Spring Boot的安全性如何?

首先,我试着阅读文档。

如果在类路径上存在Spring Security,则默认情况下,所有HTTP端点都启用了“BASIC”认证。要按方法添加安全性,请在所需的配置上添加@EnableGlobalMethodSecurity。

仅仅通过添加spring-boot-starter-security的依赖,就可以实现启用安全功能,是这个意思吗?

默认的安全设置是由SecurityAutoConfiguration类和其导入的类(SpringBootWebSecurityConfiguration用于Web安全,AuthenticationManagerConfiguration用于认证设置,也可用于非Web应用程序)中实现的。要完全禁用默认的Web应用程序安全设置,需要添加一个带有@EnableWebSecurity注解的Bean(这不会禁用认证管理器设置或Actuator的安全设置)。要对其进行自定义(例如添加基于表单的登录),通常可以使用外部属性或使用WebSecurityConfigurerAdapter类型的Bean。要禁用认证管理器设置,可以添加一个AuthenticationManager类型的Bean,或者使用被注解@Configuration的类的自动装配AuthenticationManagerBuilder来设置全局的AuthenticationManager。

添加继承WebSecurityConfigurerAdaptor的bean是关键。

接下来,我们将跟着指南,开始制作登录界面。

修改pom.xml文件。

我们将使用Thymeleaf模板和安全功能。

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

创建与设置相关的类

WebSecurityConfigurerAdapter继承类

除了/login和/logout,其他都需要进行身份验证。

package com.example.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

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

WebMvcConfigurerAdaptor继承类。

设置URL和视图文件。

package com.example.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

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

创建视图

由于Thymeleaf模板引擎似乎是使用SAX来加载视图文件的,所以必须将其制作为XML格式。

首屏

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" 
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <head>
        <title>HOME</title>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    </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>

登录页面

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" 
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <head>
        <title>LOGIN</title>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    </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>

进行构建并执行

运行后,将输出自动生成的用户密码。

登录ID

Screenshot from 2016-10-31 18-40-16.png

当访问 http://localhost:8080/ 时,会被重定向到登录页面。

Screenshot from 2016-10-31 18-40-47.png

当登录ID或密码不正确时,会跳转到login?error页面。

Screenshot from 2016-10-31 18-41-19.png

如果输入正确的登录ID和密码,则会显示主页。

Screenshot from 2016-10-31 18-41-45.png

登出后,将重定向到login?logout页面。

Screenshot from 2016-10-31 18-42-03.png

请参考以下链接。

    • 実装サンプル集

 

    Getting Started – Securing a web applicaiton
因为不知道是什么情况,所以直接不翻译。