2015-03-25 13 views
5

मुझे अपने वसंत आवेदन के साथ कोई समस्या है क्योंकि मैंने कुछ सुरक्षा चीजों को शामिल करने का प्रयास किया है।प्रमाणीकरण के साथ वसंत बूट - लॉगिन पृष्ठ नहीं मिला (404)

कोणीय जेएस सहित एक छोटे से काम करने वाले ऐप के निर्माण के बाद मैंने इस spring security tutorial का पालन किया लेकिन मैं इसे शुरू नहीं कर सकता। जब मैं ऐप के किसी भी भाग तक पहुंचने का प्रयास करता हूं, तो सुरक्षा मॉड्यूल http://localhost:8080/login पर रीडायरेक्ट करना चाहता है ... लेकिन इसे नहीं मिल रहा है।

एक अनपेक्षित त्रुटि (प्रकार = नहीं मिला, स्थिति = 404) था। कोई संदेश उपलब्ध

हो सकता है कि मैं सिर्फ एक छोटे से बात याद कर रहा हूँ, लेकिन मैं समझ नहीं कि यह क्या है ^^

यहाँ मेरी कोड है ...

फ़ोल्डर संरचना:

src/main/java 
+-Application.java 
+-SecurityConfiguration.java 

src/main/resources 
+-static 
    +-index.html 
+-templates 
    +-login.html 

pom.xml:

<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.3.0.BUILD-SNAPSHOT</version> 
</parent> 

<!-- Additional lines to be added here... --> 
<dependencies> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-thymeleaf</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-data-jpa</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-security</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.postgresql</groupId> 
     <artifactId>postgresql</artifactId> 
     <version>9.3-1100-jdbc4</version> 
    </dependency> 
    <dependency> 
     <groupId>com.googlecode.json-simple</groupId> 
     <artifactId>json-simple</artifactId> 
    </dependency> 

</dependencies> 

Application.java:

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; 

@Configuration 
@EnableAutoConfiguration 
@ComponentScan 
public class Application { 

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

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

SecurityConfiguration.java:

import org.springframework.context.annotation.Configuration; 
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 
import org.springframework.security.config.annotation.web.builders.HttpSecurity; 
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity; 
import org.springframework.security.web.util.matcher.AntPathRequestMatcher; 

@Configuration 
@EnableWebMvcSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
       .authorizeRequests().anyRequest().authenticated(); 
     http 
       .formLogin().failureUrl("/login?error") 
       .defaultSuccessUrl("/") 
       .loginPage("/login") 
       .permitAll() 
       .and() 
       .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/login") 
       .permitAll(); 
    } 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.inMemoryAuthentication().withUser("user").password("password").roles("USER"); 
    } 
} 

login.html:

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://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="@{/login}" 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> 

उत्तर

5

मैं अपने खुद के संकेत का पालन किया ऊपर टिप्पणी में और पता लगा कि मैं अपने Application.class

समस्या हल यही पर extends WebMvcConfigurerAdapter भूल गया!

0

आपके login.html कहां स्थित है? क्या आप सीधे ब्राउज़र में /login प्राप्त कर सकते हैं? मुझे संदेह है कि आपके पास ViewResolver ठीक से सेट नहीं है। यदि आप सीधे /login पर नहीं जा सकते हैं, तो यह आपकी समस्या है। Application.properties मान spring.view.prefix और spring.view.suffix पर देखें।

यदि आप सीधे वहां पहुंच सकते हैं, तो आप एक छिपी हुई त्रुटि से निपट सकते हैं। स्प्रिंग बूट एक स्वचालित कि अपवाद निगल और फेंकता एक 404. आप को बाहर करने के लिए यह (@EnableAutoConfiguration(exclude=ErrorMvcAutoConfiguration.class) को @EnableAutoConfiguration बदल चाहते हो सकता है भी शामिल है। यही कारण है कि किसी भी त्रुटि के उस अनुरोध प्रसंस्करण के दौरान फेंक दिया जाता है देखने के लिए कर सकेंगे।

+0

मेरे 'login.html' IST' src/मुख्य/संसाधन/templates' में स्थित है और मैं भी करने के लिए इसे कॉपी किया ' src/main/resource/static' (जहां सभी अन्य .html फ़ाइलें हैं) लेकिन मैं dir नहीं जा सकता ectly '/ login' या तो। ट्यूटोरियल - भाग ** _ लॉगिन एक्शन पंजीकृत करना _ ** कहता है कि मुझे बस अपने आवेदन कॉन्फ़िगरेशन में इस मैपिंग को निर्दिष्ट करने की आवश्यकता है ... जैसा मैंने किया था। एकमात्र चीज यह है कि, जब मैं '@ ओवरराइड 'का उपयोग करना चाहता हूं तो मुझे एक ग्रहण त्रुटि मिलती है, विधि प्रकार के व्यूव्यू कंट्रोलर (व्यू कंट्रोलर रजिस्ट्री) विधि को एक सुपरटेप विधि \t एप्लिकेशन.जावा' को ओवरराइड या कार्यान्वित करना होगा। इसलिए मैंने बस '@ ओवरराइड' हटा दिया –

संबंधित मुद्दे