2016-08-09 116 views
-1

我正在學習Spring引導,雖然我可以運行我的項目,但當我嘗試訪問它時,希望獲得一個空白頁面,我得到一個登錄名/密碼提示,儘管我已在我的應用程序中定義了一些值。屬性文件,我總是得到一個錯誤。如何登錄到我的新彈簧啓動應用程序?

pom.xml是:

<?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>b.c.g</groupId> 
    <artifactId>app</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 

    <name>appName</name> 
    <description>appDescription</description> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.4.0.RELEASE</version> 
     <relativePath/> <!-- lookup parent from repository --> 
    </parent> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
     <java.version>1.8</java.version> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-actuator</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-cache</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-jersey</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.projectlombok</groupId> 
      <artifactId>lombok</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-mail</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-mobile</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-social-facebook</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-social-twitter</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-web</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web-services</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-tomcat</artifactId> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.restdocs</groupId> 
      <artifactId>spring-restdocs-mockmvc</artifactId> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

application.properties是:

spring.datasource.url = jdbc:mysql://localhost:3306/app 
spring.datasource.name=app 
spring.datasource.username=app 
spring.datasource.password=appPassword 
spring.datasource.driver-class-name=com.mysql.jdbc.Driver 
spring.jpa.database=mysql 
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect 

security.user.name=master 
security.user.password=abc123 

最後,我的主類是:

@Configuration 
@EnableAutoConfiguration 
@ComponentScan 
@Controller 
public class AppNameApplication { 

    @ResponseBody 
    @RequestMapping(value = "/") 
    public String entry() { 
     return "AppName"; 
    } 

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

雖然我是一個有經驗的Spring用戶,我學習Spring引導以增加我的開發技能。

我得到在瀏覽器上的錯誤是:

白色標籤錯誤頁面

該應用對/錯誤沒有明確的映射,所以你看到 此作爲後備。 Tue Aug 09 13:33:59 BRT 2016有一個 意外錯誤(type = Unauthorized,status = 401)。 Bad credentials

我知道現在沒有什麼事情了,但這是預期的行爲嗎?我不應該,至少能夠登錄嗎?

+0

你是什麼意思登錄?你的應用的哪個部分管理用戶和/或認證他們? –

+3

您正在使用spring安全啓動程序,因此我認爲您可以先嚐試刪除它,以檢查是否可以訪問您的端點。如果可行,那麼你需要學習如何配置彈簧安全。順便說一句,如果你想學習SpringBoot一步一步開始,而不是在一次拍攝中添加所有內容。 –

+0

請參閱此處瞭解Spring引導安全配置:http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-security – woemler

回答

2

我會發布這個答案作爲一個更詳細的「評論」,因爲我不能評論很好的細節。

在你的pom.xml您已經添加了春季安全首發:

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

這意味着春天將啓用安全訪問您的端點,因此您未經授權錯誤。 Spring Security的默認行爲是鎖定整個應用程序

因此,您必須相應地配置彈簧安全性以使您能夠訪問。

在這個環節作爲詳細https://spring.io/guides/gs/securing-web/

你必須創建一個WebSecurity配置器,下面的例子:

@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"); 
    } 
} 

捲入與Spring Security之前,您可以先刪除安全啓動,並嘗試訪問你的端點。一旦你確認你的API按預期工作,那麼你就可以開始玩安全。同樣,我建議你從頭開始,不加入首發,並逐個添加,同時學習,而不是在一次拍攝中添加所有內容。

+1

默認的'spring-boot-starter-security'阻止一切的行爲? –

+0

@Federico Piazza,謝謝!我將從頭開始。 – gtludwig

+1

@SotiriosDelimanolis @SotiriosDelimanolis,我不能說'是',因爲我會說謊,但我記得我面臨同樣的問題,並修正它時,添加web配置與'permitAll' –

相關問題