Spring启动自定义登录:“登录”,模板可能不存在,或者任何配置的模板解析器都可能无法访问

问题描述 投票:1回答:1

我正在尝试使用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";
    }

}

项目结构

enter image description here

运行此应用程序并在浏览器中点击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

任何建议表示赞赏!

谢谢!

enter image description here

java spring-boot spring-security
1个回答
0
投票

这个问题来自Thymeleaf。它告诉你它无法在“templates”目录中找到“登录”页面。

enter image description here

还要检查页面是否有正确的配置:

<html xmlns:th="http://www.thymeleaf.org">
© www.soinside.com 2019 - 2024. All rights reserved.