2016-03-29 9 views
22

मेरे पास स्प्रिंग सुरक्षा कॉन्फ़िगर के साथ एक वसंत बूट वेब ऐप है। मैं थोड़ी देर के लिए प्रमाणीकरण अक्षम करना चाहता हूं (जब तक आवश्यक हो)।वसंत बूट ऐप में वसंत सुरक्षा को अक्षम करना

मैं application.properties से जोड़ें:

security.basic.enable: false 
management.security.enabled: false 

यहाँ मेरी

का कुछ हिस्सा है लेकिन मैं अभी भी एक बुनियादी सुरक्षा शामिल है: वहाँ स्टार्टअप पर उत्पन्न एक डिफ़ॉल्ट सुरक्षा पासवर्ड है और मैं अभी भी कर रहा हूँ HTTP प्रमाणीकरण प्रॉम्प्ट बॉक्स प्राप्त करना।

मेरे pom.xml:

<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>fr.test.sample</groupId> 
    <artifactId>navigo</artifactId> 
    <version>1.0.0-SNAPSHOT</version> 

    <!-- Inherit defaults from Spring Boot --> 
    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.3.1.RELEASE</version> 
    </parent> 

    <properties> 
     <java.version>1.7</java.version> 
     <jsoup.version>1.8.3</jsoup.version> 
     <guava.version>18.0</guava.version> 
     <postgresql.version>9.3-1103-jdbc41</postgresql.version> 
    </properties> 

    <!-- Add typical dependencies for a web application --> 
    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-actuator</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-mail</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context-support</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.velocity</groupId> 
      <artifactId>velocity</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-devtools</artifactId> 
      <optional>true</optional> 
     </dependency> 
     <dependency> 
      <groupId>org.jsoup</groupId> 
      <artifactId>jsoup</artifactId> 
      <version>${jsoup.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>com.google.guava</groupId> 
      <artifactId>guava</artifactId> 
      <version>${guava.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-security</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-jpa</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.postgresql</groupId> 
      <artifactId>postgresql</artifactId> 
      </dependency> 
    </dependencies> 

    <!-- Package as an executable jar --> 
    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 

    <!-- Add Spring repositories --> 
    <!-- (you don't need this if you are using a .RELEASE version) --> 
    <repositories> 
     <repository> 
      <id>spring-snapshots</id> 
      <url>http://repo.spring.io/snapshot</url> 
      <snapshots> 
       <enabled>true</enabled> 
      </snapshots> 
     </repository> 
     <repository> 
      <id>spring-milestones</id> 
      <url>http://repo.spring.io/milestone</url> 
     </repository> 
    </repositories> 
    <pluginRepositories> 
     <pluginRepository> 
      <id>spring-snapshots</id> 
      <url>http://repo.spring.io/snapshot</url> 
     </pluginRepository> 
     <pluginRepository> 
      <id>spring-milestones</id> 
      <url>http://repo.spring.io/milestone</url> 
     </pluginRepository> 
    </pluginRepositories> 

</project> 

सुरक्षा WebSecurityConfig.java में कॉन्फ़िगर किया गया है (मैं एनोटेशन टिप्पणी की है उसे निष्क्रिय करने के लिए):

//@Configuration 
//@EnableWebSecurity 
//@EnableGlobalMethodSecurity(prePostEnabled = true) 
//@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 
    @Autowired 
    UserDetailsService userDetailsService; 

    @Autowired 
    UserService userService; 

    @Autowired 
    private DataSource datasource; 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     // http.authorizeRequests().antMatchers("/bus/topologie", "/home") 
     // http.authorizeRequests().anyRequest().authenticated() 
     // .antMatchers("/admin/**").access("hasRole('ADMIN')").and() 
     // .formLogin().failureUrl("/login?error") 
     // .defaultSuccessUrl("/bus/topologie").loginPage("/login") 
     // .permitAll().and().logout() 
     // .logoutRequestMatcher(new AntPathRequestMatcher("/logout")) 
     // .logoutSuccessUrl("/login").permitAll().and().rememberMe() 
     // .rememberMeParameter("remember-me") 
     // .tokenRepository(persistentTokenRepository()) 
     // .tokenValiditySeconds(86400).and().csrf(); 
    } 

    @Bean 
    public PersistentTokenRepository persistentTokenRepository() { 
     JdbcTokenRepositoryImpl tokenRepositoryImpl = new JdbcTokenRepositoryImpl(); 
     tokenRepositoryImpl.setDataSource(datasource); 
     return tokenRepositoryImpl; 
    } 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) 
      throws Exception { 

     PasswordEncoder encoder = new BCryptPasswordEncoder(); 

     auth.userDetailsService(userDetailsService).passwordEncoder(encoder); 
     auth.jdbcAuthentication().dataSource(datasource); 

     if (!userService.userExists("user")) { 
      User userAdmin = new User("user", encoder.encode("password"), true); 
      Set<Authorities> authorities = new HashSet<Authorities>(); 
      authorities.add(new Authorities(userAdmin,"ADMIN")); 
      authorities.add(new Authorities(userAdmin,"CRIP")); 
      authorities.add(new Authorities(userAdmin,"USER")); 
      userAdmin.setAuthorities(authorities); 

      userService.createUser(userAdmin); 
     } 
    } 

} 
+0

अधिक जानकारी जोड़ें, अपने 'pom.xml' या 'build.gradle', सुरक्षा जावा कॉन्फ़िगरेशन और अन्य प्रासंगिक कोड –

+1

http: //stackoverflow.com/questions/23894010/spring-boot-security-disable-security – soorapadman

उत्तर

46

उपयोग security.ignored संपत्ति है:

security.ignored=/** 

security.basic.enable: false सिर्फ सुरक्षा ऑटो विन्यास के कुछ हिस्से को निष्क्रिय कर देगा लेकिन अपने WebSecurityConfig अभी भी पंजीकृत किया जाएगा।

है स्टार्टअप पर उत्पन्न एक डिफ़ॉल्ट सुरक्षा पासवर्ड

AutowiredAuthenticationManagerBuilder करने का प्रयास करें:

@Override 
@Autowired 
protected void configure(AuthenticationManagerBuilder auth) throws Exception { ... } 
+0

सुरक्षा कॉन्फ़िगर क्लास या application.properties में जाने के लिए security.ignored =/** है? –

+1

धन्यवाद, "security.ignored =/**" काम किया। –

9

मुझे लगता है कि आप भी सुरक्षा ऑटो निकालना होगा अपने @SpringBootApplication एनोटेट वर्ग से config:

@EnableAutoConfiguration(exclude = { 
    org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration.class, 
    org.springframework.boot.actuate.autoconfigure.ManagementSecurityAutoConfiguration.class}) 
7

इस प्रयास करें। एक नई कक्षा

@Configuration 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(HttpSecurity httpSecurity) throws Exception { 
     httpSecurity.authorizeRequests().antMatchers("/").permitAll(); 
} 

} 

असल में यह वसंत को प्रत्येक यूआरएल तक पहुंच की अनुमति देने के लिए बताता है। @Configuration वसंत बताता है कि यह एक विन्यास वर्ग

+0

मुझे antoconfigure.security और .permitAll() एंटीमैचर्स पर दोनों बहिष्करण कथन जोड़कर जाना है। –

+0

\ @EnableWebSecurity की जरूरत है \ @EnableWebSecurity संरक्षित स्थिर वर्ग SecurityConfiguration – Dexter

+0

तुम भी इतनी है कि आप जब तक आप प्रोफ़ाइल "nosecure" निर्दिष्ट कर सकते हैं '@Profile (" nosecure ") की तरह कुछ के साथ इस तरह एक वर्ग टिप्पणी कर सकते हैं' इसे चालू करना चाहते हैं। – Mark

0

अपने सुरक्षा विन्यास वर्ग पर उपयोग @profile("whatever-name-profile-to-activate-if-needed") कि फैली WebSecurityConfigurerAdapter

security.ignored=/** 

security.basic.enable: false 

एनबी। मुझे यह जानने के लिए डीबग करने की जरूरत है कि क्यों ऑटो कॉन्फ़िगरेशन को बाहर रखा गया मेरे लिए काम नहीं किया। लेकिन प्रोफाइल बहुत खराब है क्योंकि आप अभी भी कॉन्फ़िगरेशन गुणों के माध्यम से इसे फिर से सक्रिय कर सकते हैं यदि आवश्यक