Skip to content

Commit 46f17ad

Browse files
committed
本例主要演示如何自定义eureka页面,以及自定义eureka登陆登出
1 parent 4a69045 commit 46f17ad

File tree

14 files changed

+465
-0
lines changed

14 files changed

+465
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<module>springboot-mybatisplus-tenant</module>
3939
<module>springboot-xxl-job-executor</module>
4040
<module>springboot-aop-spel</module>
41+
<module>springboot-custom-eureka</module>
4142
</modules>
4243
<parent>
4344
<groupId>org.springframework.boot</groupId>

springboot-custom-eureka/README.MD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
本例主要演示如何自定义eureka页面,以及自定义eureka登陆登出
2+
3+
## 自定义登陆
4+
5+
实现核心点:通过spring-security进行集成登陆
6+
7+
## 自定义eureka页面
8+
9+
实现核心点:重写spring-cloud-netflix-eureka-server-*.jar中的templates/eureka下的页面
10+
11+
**注:** 不同版本的eureka页面略有区别

springboot-custom-eureka/pom.xml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>springboot-learning</artifactId>
7+
<groupId>com.github.lybgeek</groupId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>springboot-custom-eureka</artifactId>
13+
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.springframework.boot</groupId>
18+
<artifactId>spring-boot-starter-web</artifactId>
19+
</dependency>
20+
<dependency>
21+
<groupId>org.springframework.cloud</groupId>
22+
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-security</artifactId>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-actuator</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>io.micrometer</groupId>
38+
<artifactId>micrometer-registry-prometheus</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>com.alibaba</groupId>
42+
<artifactId>fastjson</artifactId>
43+
</dependency>
44+
45+
46+
</dependencies>
47+
48+
49+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.github.lybgeek.eureka.server;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
6+
7+
/**
8+
* @description: eureka服务端
9+
*
10+
**/
11+
@SpringBootApplication
12+
@EnableEurekaServer
13+
public class EurekaServerApplication {
14+
public static void main(String[] args) {
15+
SpringApplication.run(EurekaServerApplication.class,args);
16+
}
17+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.github.lybgeek.eureka.server.config;
2+
3+
import io.micrometer.core.instrument.MeterRegistry;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
9+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
10+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
11+
12+
/**
13+
* @description: eureka服务端 csrf过滤eureka路径
14+
*
15+
**/
16+
@EnableWebSecurity
17+
@Configuration
18+
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
19+
@Override
20+
protected void configure(HttpSecurity http) throws Exception {
21+
http.csrf().disable()
22+
.authorizeRequests()
23+
.antMatchers("/toLogin*","/login*","/css/*","/js/*","/actuator/**").permitAll()
24+
.and()
25+
.formLogin()
26+
.loginPage("/toLogin")
27+
.loginProcessingUrl("/login").failureUrl("/login/error").permitAll();
28+
super.configure(http);
29+
}
30+
31+
@Bean
32+
MeterRegistryCustomizer<MeterRegistry> configure(@Value("${spring.application.name}") String applicationName){
33+
return registry -> registry.config().commonTags("application", applicationName);
34+
}
35+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.github.lybgeek.eureka.server.controller;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.security.authentication.*;
5+
import org.springframework.security.core.AuthenticationException;
6+
import org.springframework.security.core.userdetails.UsernameNotFoundException;
7+
import org.springframework.security.web.WebAttributes;
8+
import org.springframework.stereotype.Controller;
9+
import org.springframework.web.bind.annotation.GetMapping;
10+
11+
import javax.servlet.http.HttpServletRequest;
12+
13+
/**
14+
* @description: 登录
15+
*
16+
**/
17+
@Controller
18+
@Slf4j
19+
public class LoginController {
20+
21+
@GetMapping("/login/error")
22+
public String loginError(HttpServletRequest request) {
23+
AuthenticationException authenticationException = (AuthenticationException) request.getSession().getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
24+
log.info("authenticationException={}", authenticationException);
25+
String errorMsg;
26+
27+
if (authenticationException instanceof UsernameNotFoundException || authenticationException instanceof BadCredentialsException) {
28+
errorMsg = "用户名或密码错误";
29+
} else if (authenticationException instanceof DisabledException) {
30+
errorMsg = "用户已被禁用";
31+
} else if (authenticationException instanceof LockedException) {
32+
errorMsg = "账户被锁定";
33+
} else if (authenticationException instanceof AccountExpiredException) {
34+
errorMsg = "账户过期";
35+
} else if (authenticationException instanceof CredentialsExpiredException) {
36+
errorMsg = "证书过期";
37+
} else {
38+
errorMsg = "登录失败";
39+
}
40+
request.setAttribute("errorMsg",errorMsg);
41+
return "login";
42+
}
43+
44+
45+
@GetMapping("/toLogin")
46+
public String login(HttpServletRequest request) {
47+
return "login";
48+
}
49+
50+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
eureka:
2+
#此处设置会改变eureka控制台的显示
3+
datacenter: ${DATA_CENTER:LYBGEEK DATA CENTER}
4+
#此处设置会改变eureka控制台的显示
5+
environment: ${ENV:dev}
6+
instance:
7+
# 服务实例ID
8+
instance-id: ${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${server.port}}
9+
client:
10+
service-url:
11+
# Eureka客户端与Eureka服务端的交互地址
12+
defaultZone: http://lybgeek:lybgeek@localhost:8761/eureka/
13+
# 是否从eureka server抓取服务信息
14+
fetch-registry: false
15+
# 是否注册到eureka server
16+
register-with-eureka: false
17+
18+
19+
server:
20+
port: 8761
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
spring:
2+
profiles:
3+
# 配置文件激活
4+
active: standalone
5+
security:
6+
user:
7+
# 认证的用户名
8+
name: lybgeek
9+
# 认证的密码
10+
password: lybgeek
11+
application:
12+
# 应用名
13+
name: custom-eureka
14+
freemarker:
15+
prefer-file-system-access: false
16+
17+
#放开权限,可以被hystrix dashboard,spring boot admin监控到
18+
management:
19+
endpoints:
20+
web:
21+
exposure:
22+
include: '*'
23+
endpoint:
24+
health:
25+
show-details: ALWAYS
26+
metrics:
27+
tags:
28+
application: ${spring.application.name}
29+
30+

springboot-custom-eureka/src/main/resources/static/css/bootstrap.min.css

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
body {
2+
padding-top: 40px;
3+
padding-bottom: 40px;
4+
background-color: #eee;
5+
}
6+
7+
.form-signin {
8+
max-width: 330px;
9+
padding: 15px;
10+
margin: 0 auto;
11+
}
12+
.form-signin .form-signin-heading,
13+
.form-signin .checkbox {
14+
margin-bottom: 10px;
15+
}
16+
.form-signin .checkbox {
17+
font-weight: 400;
18+
}
19+
.form-signin .form-control {
20+
position: relative;
21+
box-sizing: border-box;
22+
height: auto;
23+
padding: 10px;
24+
font-size: 16px;
25+
}
26+
.form-signin .form-control:focus {
27+
z-index: 2;
28+
}
29+
.form-signin input[type="email"] {
30+
margin-bottom: -1px;
31+
border-bottom-right-radius: 0;
32+
border-bottom-left-radius: 0;
33+
}
34+
.form-signin input[type="password"] {
35+
margin-bottom: 10px;
36+
border-top-left-radius: 0;
37+
border-top-right-radius: 0;
38+
}

0 commit comments

Comments
 (0)