spring - 使用 thymeleaf 无法识别 Spring Security 自定义 HTML 登录页面
问题描述
我已按照此链接进行所有配置,以设置链接我的自定义 html 页面以进行登录。但是,当我访问 localhost/login 时,我遇到了错误状态 500。
当我访问 localhost:8080/login 时,我无法呈现简单的 html 页面。
- 是否需要额外的配置?
- html 页面是否应该位于模板文件夹中?应用程序如何知道它应该呈现“login.html”?
- 我的控制器是否被识别?
配置
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().fullyAuthenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll();
}
}
控制器
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class LoginController {
@GetMapping("/login")
public String login() {
return "login";
}
}
将上面的“登录”替换为下面也会显示错误。
<h1> Login page <h1>
pom.xml 中的依赖项
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-ldap</artifactId>
</dependency>
<dependency>
<groupId>com.unboundid</groupId>
<artifactId>unboundid-ldapsdk</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
<version>1.4.193</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
login.html 位于模板中。错误信息:
javax.servlet.ServletException: Circular view path [login]: would dispatch back to the current handler URL [/login] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
解决方案
@RestController
是一个组合注释,它本身带有元注释@Controller
并@ResponseBody
指示一个控制器,其每个方法都继承类型级别的 @ResponseBody 注释,因此直接写入响应正文与视图分辨率并使用 HTML 模板进行渲染。
这就是为什么我建议将 @Controller 替换为@RestController
.
您不会在 html 文件中获得静态资源。要得到这个,你必须添加这个。
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/resources/**", "/static/**", "/css/**", "/js/**", "/images/**");
}
推荐阅读
- go - 查找 2 个语句之间的匹配百分比
- bash - lftp、stdout、stderr 和 SAP exec_protocol
- javascript - 使用 cookie 连接到 websocket
- mysql - 我是否正确使用“NOT IN”?
- c# - Unity2d:dontDestroyOnLoad 只工作一次吗?
- python - Python Sphinx:如何将代码嵌入到文档字符串中?
- arrays - 试图理解循环
- java - 在 NetBeans 中将 MySQL Workbench 数据库连接到 Java 应用程序
- javascript - 在 React 上使用多个复选框和钩子
- firebase - 如果在 TextFormField 中未输入任何值,如何将字段保存为“null”到 Firebase