Spring安全错误循环视图路径[index]:将再次分派回当前的处理程序URL [/ index]。检查您的ViewResolver设置

时间:2019-12-21 11:37:22

标签: spring-boot spring-security

使用gradle运行spring boot security应用程序时出现以下错误-

javax.servlet.ServletException:循环视图路径[index]:将再次分派回当前处理程序URL [/ index]。检查您的ViewResolver设置! (提示:由于默认视图名称的生成,这可能是未指定视图的结果。)     在org.springframework.web.servlet.view.InternalResourceView.prepareForRendering(InternalResourceView.java:210)[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]     在org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:148)[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]     在org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:316)[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]     在org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1373)[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]     在org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1118)[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]     在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1057)[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]     在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]     在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]     在org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]     在javax.servlet.http.HttpServlet.service(HttpServlet.java:634)[tomcat-embed-core-9.0.29.jar:9.0.29]     在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]     在javax.servlet.http.HttpServlet.service(HttpServlet.java:741)[tomcat-embed-core-9.0.29.jar:9.0.29]

我在应用程序中添加了以下代码-

src / main / resources / templates / index.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="@{/main}">here</a> to see a greeting.</p>
    </body>
</html>

src / main / resources / templates / main.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>Hello world!</h1>
    </body>
</html>

src / main / resources / templates / mylogin.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>
        <div th:if="${param.error}">
            Invalid username and password.
        </div>
        <div th:if="${param.logout}">
            You have been logged out.
        </div>
        <form th:action="@{/mylogin}" 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>

src / main / com / example / security / MVCConfig.java

package com.example.security;

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

@Configuration
public class MVCConfig implements WebMvcConfigurer {

  public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/index").setViewName("index");
    registry.addViewController("/").setViewName("index");
    registry.addViewController("/main").setViewName("main");
    registry.addViewController("/mylogin").setViewName("mylogin");
  }
}

src / main / com / example / security / WebSecurityConfig.java


package com.example.security;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http
      .authorizeRequests()
        .antMatchers("/", "/main").permitAll()
        .anyRequest().authenticated()
        .and()
      .formLogin()
        .loginPage("/mylogin")
        .permitAll()
        .and()
      .logout()
        .permitAll();
  }

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

    return new InMemoryUserDetailsManager(user);
  }
}

src / main / com / example / security / SecurityApplication.java


package com.example.security;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SecurityApplication {

    public static void main(String[] args) {
        SpringApplication.run(SecurityApplication.class, args);
    }

}

build.gradle

plugins {
    id 'org.springframework.boot' version '2.2.2.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-web'   
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    testImplementation 'org.springframework.security:spring-security-test'
}

test {
    useJUnitPlatform()
}

1 个答案:

答案 0 :(得分:0)

您忘记了一种依赖性。请添加

implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'

依赖于您。

您可以在此处https://github.com/ozkanpakdil/spring-examples/tree/master/gradle-thymeleaf

看到运行示例