我正在尝试使用spring security实现自定义登录。所以我添加了maven依赖项。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
和我的安全配置课程
package it.mine.my.web.config;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
@Override
protected void configure( HttpSecurity http ) throws Exception
{
http
.authorizeRequests()
.antMatchers( "/login" )
.permitAll()
.anyRequest()
.authenticated()
.and()
.formLogin()
.loginProcessingUrl( "/process" )
.loginPage( "/login" )
.usernameParameter( "username" )
.passwordParameter( "password" )
.defaultSuccessUrl( "/test", true )
.permitAll();
}
}
然后我创建了一个LoginController,如下所示。
package it.mine.my.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import java.util.Locale;
@Controller
public class LoginController
{
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login( @RequestParam(value = "error", required = false) String error, @RequestParam(value = "successMsg", required = false) String success, Model model, Locale loale )
{
model.addAttribute( "error", error != null );
model.addAttribute( "errorMsg", error );
model.addAttribute( "success", success != null );
model.addAttribute( "successMsg", success );
model.addAttribute( "locale", loale.getLanguage() );
return "login";
}
}
项目结构
运行此应用程序并在浏览器中点击http://localhost:8080/login后会出现以下错误。
Intellij IDEA控制台说,
org.thymeleaf.exceptions.TemplateInputException: Error resolving template "login", template might not exist or might not be accessible by any of the configured Template Resolvers
任何建议表示赞赏!
谢谢!