2014-04-23 9 views
8

मैं प्रबंधन अंत बिंदुओं जैसे /env, /health, /metrics के लिए बुनियादी सुरक्षा का उपयोग कैसे करूं? मैं अन्य अनुप्रयोग नियंत्रक अंत बिंदु सुरक्षा की तुलना में उपरोक्त अंत बिंदुओं के लिए अलग-अलग उपयोगकर्ता प्रमाण-पत्रों का उपयोग करना चाहता हूं। मेरी application.properties फ़ाइल में, मैं आवेदन नियंत्रक सुरक्षावसंत बूट प्रबंधन अंत बिंदु मूल सुरक्षा

 
security.user.name=user 
security.user.password=password 

के लिए नीचे निर्दिष्ट लेकिन मैं प्रबंधन अंत अंक के लिए अलग अलग नाम/पासवर्ड चाहते हैं। management.security.user.name संपत्ति नहीं मिली।

उत्तर

2

वसंत सुरक्षा में "वैश्विक" AuthenticationManager@Bean प्रकार GlobalAuthenticationConfigurerAdapter के उदाहरणों में कॉन्फ़िगर किया गया है। यह AuthenticationManager वह है जो security.user.* गुणों द्वारा कॉन्फ़िगर किया गया है, जब तक कि आप security.basic.enabled=false सेट न करें। वैश्विक AM डिफ़ॉल्ट रूप से प्रबंधन अंतराल से भी जुड़ा हुआ है, और यह WebSecurityConfigurationAdapters में परिभाषित किसी भी "स्थानीय" AuthenticationManagers का अभिभावक है (वे सभी ProviderManagers हैं)।

इस प्रकार, यदि आप प्रबंधन अंतिमबिंदुओं और आवेदन अंतिमबिंदुओं के लिए अलग उपयोगकर्ता खातों चाहते हैं, आप है (कम से कम) के पास दो विकल्प:

  • एक WebSecurityConfigurationAdapter में आपके आवेदन अंतिमबिंदुओं के लिए एक स्थानीय AM परिभाषित करें और सुनिश्चित करें कि प्रबंधन अंतराल उस फ़िल्टर द्वारा कवर नहीं हैं। यह आसान है, क्योंकि यह आपको बिना किसी सोच के मिलता है और AuthenticationManagerBuilder को अपने WebSecurityConfigurationAdapter में जोड़ता है (जब तक इसे filter that secures the management endpoints के संबंध में सावधानीपूर्वक आदेश दिया जाता है)।

  • वैश्विक AM (या वास्तव में एक अन्य स्थानीय एक) आवेदन अंतिमबिंदुओं के लिए उपयोग करें और प्रबंधन अंतिमबिंदुओं के लिए सुरक्षा को पुन: कॉन्फ़िगर (जैसे security.basic.enabled=false सेट और अपने खुद के WebSecurityConfigurerAdapter प्रबंधन अंतिमबिंदुओं कवर जोड़ें)। यह अधिक काम हो सकता है, और कुछ बूट डिफ़ॉल्ट को डुप्लिकेट करता है, लेकिन कम से कम आपको पता चलेगा कि आप क्या प्राप्त कर रहे हैं।

1

डेव पहले से ही अच्छी तरह से बताया गया है, लेकिन यहाँ प्रमाणन स्रोत के रूप में WebSecurityConfigurerAdapter और डेटाबेस का उपयोग कर के साथ कुछ पूर्ण उदाहरण।

SecurityConfig.java

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private DataSource dataSource; 

    @Override 
    public void configure(WebSecurity web) throws Exception { 
     // Ignore any request that starts with /resources or /webjars 
     web.ignoring() 
      .antMatchers("/resources/**") 
      .antMatchers("/webjars/**"); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.csrf().disable(); 
     // for app access 
     http.authorizeRequests() 
      .antMatchers("/configuration").hasRole("ADMIN") 
      .antMatchers("/user").hasRole("ADMIN") 
      .anyRequest().fullyAuthenticated() 
      .and() 
      .exceptionHandling().accessDeniedPage("/auth_error") 
      .and() 
      .formLogin().loginPage("/login").failureUrl("/login?error").permitAll() 
      .and() 
      .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/").invalidateHttpSession(true); 
     // for management access with basic auth 
     http.httpBasic() 
      .and() 
      .authorizeRequests() 
      .antMatchers("/management/**").hasRole("ADMIN"); 
    } 

    @Override 
    public void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.jdbcAuthentication().dataSource(dataSource) 
      .passwordEncoder(new BCryptPasswordEncoder()); 
    } 
} 

और ये मेरे application.properties है

application.properties

# MANAGEMENT HTTP SERVER (ManagementServerProperties) 
management.port=8081 
management.address=127.0.0.1 
management.context-path=/management 
management.security.enabled=true 

# MVC ONLY ENDPOINTS 
endpoints.jolokia.path=/jolokia 
endpoints.jolokia.sensitive=true 
endpoints.jolokia.enabled=true 

# JMX ENDPOINT (EndpointMBeanExportProperties) 
endpoints.jmx.enabled=true 
endpoints.jmx.domain=org.springboot 
endpoints.jmx.unique-names=false 

# ENDPOINT 
endpoints.enabled=true 

endpoints.shutdown.id=shutdown 
endpoints.shutdown.sensitive=true 
endpoints.shutdown.enabled=true 

# HYPERMEDIA ENDPOINTS 
endpoints.actuator.enabled=true 
endpoints.actuator.path=/actuator 
endpoints.actuator.sensitive=false 

आप spring application properties

से अधिक अंतिमबिंदुओं गुण जाँच कर सकते हैं

प्रबंधन अनुरोध उदाहरण

वहाँ नियंत्रक भूमिका उपयोगकर्ता (उपयोगकर्ता नाम: व्यवस्थापक, पासवर्ड: पासवर्ड) पहले से ही डेटाबेस पर शामिल किया।

  • jolokia के माध्यम से HeapMemoryUsage और THREADCOUNT की जाँच के लिए

    $ curl -u admin:password -X POST http://127.0.0.1:8081/management/shutdown 
    {"message":"Shutting down, bye..."} 
    
  • उदाहरण प्रबंधन अनुरोध को बंद करने के लिए उदाहरण प्रबंधन अनुरोध

    $ curl -u admin:password http://127.0.0.1:8081/management/jolokia/read/java.lang:type=Memory/HeapMemoryUsage 
    {"request":{"mbean":"java.lang:type=Memory","attribute":"HeapMemoryUsage","type":"read"},"value":{"init":268435456,"committed":829947904,"max":3817865216,"used":466033000},"timestamp":1444167809,"status":200} 
    
    
    $ curl -u admin:password http://127.0.0.1:8081/management/jolokia/read/java.lang:type=Threading/ThreadCount 
    {"request":{"mbean":"java.lang:type=Threading","attribute":"ThreadCount","type":"read"},"value":47,"timestamp":1444174639,"status":200} 
    
  • स्वास्थ्य

    की जाँच के लिए उदाहरण प्रबंधन अनुरोध
    $ curl -u admin:password http://127.0.0.1:8081/management/health 
    {"status":"UP","diskSpace":{"status":"UP","free":163634987008,"threshold":10485760},"db":{"status":"UP","database":"H2","hello":1}} 
    
2

अंत अंक बुनियादी सुरक्षा लागू करने के लिए आप की तरह एक

@Configuration 
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.csrf().disable().authorizeRequests().anyRequest().authenticated().and().httpBasic(); 
    } 
} 

नीचे अभी भी तो काम नहीं कर रहा होना चाहिए कोड

security.user.name=user 
security.user.password=password 

नीचे और विन्यास फाइल में उपयोग करने की आवश्यकता उम्मीद है कि इस काम करेंगे

Basic Authentication

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