2017-04-30 79 views
0

當前嘗試創建一個簡單的登錄與春季安全,但我得到一個錯誤,我認爲是因爲我做錯了解析器視圖或控制器的東西。只是似乎無法弄清楚什麼。我收到以下錯誤設置彈簧安全困境的自定義登錄頁面

「出現意外錯誤(類型=內部服務器錯誤,狀態= 500) 圓形視圖路徑[home]:將重新返回當前處理程序URL [/ home]。您的ViewResolver設置!(提示:這可能是一個未指定的視圖的結果,由於默認視圖名稱生成。)」

我的配置:

@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter{ 

    public void configure(AuthenticationManagerBuilder auth) throws Exception { 

     auth.inMemoryAuthentication() 
       .withUser("user") 
       .password("password") 
       .roles("USER"); 
    } 

    public void configureGlobal(HttpSecurity http) throws Exception { 


     http.authorizeRequests() 
       .antMatchers("/resources/**").permitAll() 
       .anyRequest().authenticated() 
       .and() 
       .formLogin() 
       .loginPage("/loginpage") 
       .permitAll().and() 
       .logout(); 
    } 

    @Bean 
    public ViewResolver viewResolver(){ 
     InternalResourceViewResolver resolver = new InternalResourceViewResolver(); 
     resolver.setViewClass(JstlView.class); 
     resolver.setPrefix("/resources/templates"); 
     resolver.setSuffix(".html"); 
     return resolver; 
    } 

    public void addViewController(ViewControllerRegistry registry){ 
     registry.addViewController("/home").setViewName("home"); 
     registry.addViewController("/loginpage").setViewName("loginpage"); 
    } 
} 

控制器:

@Controller 
public class loginController { 

    @RequestMapping("/home") 
    public String homePage(Model model){ 

     return "home"; 
    } 

    @GetMapping("/loginpage") 
    public String loginGetter(Model model){ 

     return "loginpage"; 
    } 
} 

Login page: 

<!DOCTYPE html> 
<html lang="en" xmlns:th="http://www.thymeleaf.org"> 
<head> 
    <title>Title</title> 
</head> 
<body> 
<div th:if="${param.error}"> 
Something is wrong 
</div> 
<div th:if="${param.logout}"> 
You have logged out 
</div> 
    <div> 
     <form action="@{loginpage}" method="post"> 
      <div><label>Username: <input type="text" name="username"/></in></label></div> 
      <div><label>Password: <input type="text" name="password"/></label></div> 
      <div><input type="submit" value="Sign in"/></in></div> 
     </form> 
    </div> 
</body> 
</html> 

我的雙響炮:

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.ronone</groupId> 
    <artifactId>SecurityTutorial</artifactId> 
    <version>1.0-SNAPSHOT</version> 

    <dependencies> 
     <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web --> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
      <version>1.5.2.RELEASE</version> 
     </dependency> 

     <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-core --> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-core</artifactId> 
      <version>4.2.2.RELEASE</version> 
     </dependency> 

     <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-web --> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-web</artifactId> 
      <version>4.2.1.RELEASE</version> 
     </dependency> 

     <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-config --> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-config</artifactId> 
      <version>3.2.0.RELEASE</version> 
     </dependency> 

     <!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring3 --> 
     <dependency> 
      <groupId>org.thymeleaf</groupId> 
      <artifactId>thymeleaf-spring3</artifactId> 
      <version>3.0.3.RELEASE</version> 
     </dependency> 


    </dependencies> 


</project> 
+0

增加了pom文件 –

+1

你可以聲明@Configuration註釋到你的SecurityConfig類嗎? –

+0

@georgesvan就是這樣,謝謝兄弟 –

回答

0

試試這個:

@EnableWebSecurity 
@Configuration 
public class SecurityConfig extends WebSecurityConfigurerAdapter{ ...} 

註解與@Configuration類指示類可以通過Spring IoC容器被用作bean定義的來源。