服务管理 配置 pom.xml(继承eureka服务后面的配置)
<?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> <parent> <groupId>com.example</groupId> <artifactId>spring-cloud</artifactId> <version>2.0-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>admin-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>admin-server</name> <description>Demo project for Spring Boot</description> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.0.1.RELEASE</version> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.0.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.jolokia</groupId> <artifactId>jolokia-core</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置application.yml
server: port: 5000 #访问端口 spring: application: name: admin-server security: user: name: 'admin' password: '123456' eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ registry-fetch-interval-seconds: 5 instance: lease-renewal-interval-in-seconds: 10 health-check-url-path: /actuator/health metadata-map: user.name: ${spring.security.user.name} user.password: ${spring.security.user.password} prefer-ip-address: true management: endpoint: health: show-details: always endpoints: web: exposure: include: "*"
启动类
package com.example.adminserver; import de.codecentric.boot.admin.server.config.EnableAdminServer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 网页登录 查询启动的服务 */ @SpringBootApplication @EnableAdminServer public class AdminServerApplication { public static void main(String[] args) { SpringApplication.run(AdminServerApplication.class, args); } }
SecuritySecureConfig类:;
package com.example.adminserver; import de.codecentric.boot.admin.server.config.AdminServerProperties; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; @Configuration public class SecuritySecureConfig extends WebSecurityConfigurerAdapter { private final String adminContextPath; public SecuritySecureConfig(AdminServerProperties adminServerProperties){ this.adminContextPath = adminServerProperties.getContextPath(); } protected void configure(HttpSecurity http)throws Exception{ SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler(); successHandler.setTargetUrlParameter("redirectTo"); http.authorizeRequests() .antMatchers(adminContextPath+"/assets/**").permitAll() .antMatchers(adminContextPath+"/login").permitAll() .anyRequest().authenticated() .and() .formLogin().loginPage(adminContextPath+"/login").successHandler(successHandler).and() .logout().logoutUrl(adminContextPath+"/logout").and() .httpBasic().and() .csrf().disable(); } }
启动服务后 浏览器输入: localhost:5000 可查看所有你启动的服务