javax.servlet.ServletException:循环视图路径[login]

时间:2017-08-04 07:29:46

标签: spring spring-boot spring-security

我是Spring Boot的新手,想要将Spring Security模块添加到我以前的项目中。我跟着这个link。我的Spring Boot版本是1.5.6.RELEASE

这是安全配置

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

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
            .withUser("user").password("password").roles("USER");
    }
}

这是MVC配置:

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

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

我可以确保home.htmlhello.htmllogin.html位于resources/templates/。当我将Spring Security部件添加到之前的项目时,我还有一个处理jpa请求的控制器

@Controller
@RequestMapping("/test/pgsql")
public class TestPostgreSQLController {

    @Autowired
    private CustomerRepository customerRepository;

    @RequestMapping("/save")
    public @ResponseBody
    String process() {
        customerRepository.save(new Customer("Neo", "Chan"));
        customerRepository.save(new Customer("Luke", "Liu"));
        customerRepository.save(new Customer("Ran", "Guo"));
        customerRepository.save(new Customer("Joey", "Chen"));
        customerRepository.save(new Customer("Larry", "Huang"));
        return "Done";
    }

    @RequestMapping("/findbyid")
    public @ResponseBody String findById(@RequestParam("id") long id) {
        String result = "";
        result = customerRepository.findOne(id).toString();
        return result;
    }

    @RequestMapping("/find")
    public @ResponseBody String find(@RequestParam("lastname") String lastName) {
        String results = "";
        for (Customer bauer : customerRepository.findCustomersByLastName(lastName)) {
            System.out.println(bauer.toString());
            results = results + bauer.toString() + "<br>";
        }
        return results;
    }


}

pom.xml就像这样

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

我将项目构建为jar包。当我访问localhost:8080/homelocalhost:8080/login地址时。它抛出以下异常:

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.)

有什么建议吗?提前谢谢。

2 个答案:

答案 0 :(得分:1)

我有同样的问题。问题与pom文件有关,您应该添加Thymeleaf依赖项,然后代码才能开始工作。

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

答案 1 :(得分:0)

问这个问题已经很久了,但我在这里找到了答案]  尝试添加registry.addViewController("/login").setViewName("login.html"); 对我有用的希望有助于1