2016-12-31 302 views
0

我正在嘗試創建一個彈簧啓動REST應用程序。當我部署我的應用程序時,它需要身份驗證,並要求輸入用戶名和密碼。我怎樣才能繞過這個或如何添加用戶名和密碼進行身份驗證?彈簧啓動彈簧安全

我需要刪除pom中的安全條目嗎?

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

回答

0

無需從pom.xml中刪除安全性。在你的項目中,你可以嘗試下面的東西。嘗試創建SecurityConfig,它將擴展WebSecurityConfigurerAdapter並提供一些用戶名和密碼,稍後您可以對其進行自定義。

import org.springframework.context.annotation.Configuration; 
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 
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; 

@Configuration 
@EnableWebMvcSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 
    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth 
      .inMemoryAuthentication() 
       .withUser("user") 
        .password("user") 
        .roles("USER") 
        .and() 
       .withUser("user2") 
        .password("secret2") 
        .roles("USER"); 
    } 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests() 
       .anyRequest().fullyAuthenticated(); 
     http 
      .httpBasic(); 
     http 
      .csrf().disable(); 
    } 
} 

public @interface EnableWebMvcSecurity { 

} 
0

如果你不想使用驗證可言,你應該刪除的依賴:

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

Spring Boot Reference Guide

如果春季安全在類路徑上,那麼web應用程序默認是安全的所有HTTP端點上的'基本'認證。