V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
oracle128g
V2EX  ›  Java

开启 SpringSecurity 后 Eureka 界面无法访问

  •  
  •   oracle128g · 2018-04-20 23:22:10 +08:00 · 6125 次点击
    这是一个创建于 2417 天前的主题,其中的信息可能已经有所发展或是发生改变。

    开启 security 之前正常

    开启 security 之前正常

    开启 security 之后无法登录

    开启 security 之后无法登录

    pom.xml

    <?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>
    
    	<groupId>org.zhl.frame</groupId>
    	<artifactId>zhl-frame</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>jar</packaging>
    
    	<name>zhl-frame</name>
    	<description>Demo project for Spring Boot</description>
    
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>2.0.1.RELEASE</version>
    		<relativePath/> <!-- lookup parent from repository -->
    	</parent>
    
    	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    		<java.version>10</java.version>
    		<spring-cloud.version>Finchley.M9</spring-cloud.version>
    	</properties>
    
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-security</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.cloud</groupId>
    			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-test</artifactId>
    			<scope>test</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.security</groupId>
    			<artifactId>spring-security-test</artifactId>
    			<scope>test</scope>
    		</dependency>
    
    		<dependency>
    			<groupId>javax.xml.bind</groupId>
    			<artifactId>jaxb-api</artifactId>
    			<version>2.3.0</version>
    		</dependency>
    		<dependency>
    			<groupId>com.sun.xml.bind</groupId>
    			<artifactId>jaxb-impl</artifactId>
    			<version>2.3.0</version>
    		</dependency>
    		<dependency>
    			<groupId>org.glassfish.jaxb</groupId>
    			<artifactId>jaxb-runtime</artifactId>
    			<version>2.3.0</version>
    		</dependency>
    		<dependency>
    			<groupId>javax.activation</groupId>
    			<artifactId>activation</artifactId>
    			<version>1.1.1</version>
    		</dependency>
    
    	</dependencies>
    
    	<dependencyManagement>
    		<dependencies>
    			<dependency>
    				<groupId>org.springframework.cloud</groupId>
    				<artifactId>spring-cloud-dependencies</artifactId>
    				<version>${spring-cloud.version}</version>
    				<type>pom</type>
    				<scope>import</scope>
    			</dependency>
    		</dependencies>
    	</dependencyManagement>
    
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.springframework.boot</groupId>
    				<artifactId>spring-boot-maven-plugin</artifactId>
    			</plugin>
    		</plugins>
    	</build>
    
    	<repositories>
    		<repository>
    			<id>spring-milestones</id>
    			<name>Spring Milestones</name>
    			<url>https://repo.spring.io/milestone</url>
    			<snapshots>
    				<enabled>false</enabled>
    			</snapshots>
    		</repository>
    	</repositories>
    
    
    </project>
    

    application.yml

    # eureka.client.registerWithEureka:表示是否将自己注册到 Eureka Server,默认为 true。由于当前这个应用就是 Eureka Server,故而设为 false
    # eureka.client.fetchRegistry:表示是否从 Eureka Server 获取注册信息,默认为 true。因为这是一个单点的 Eureka Server,不需要同步其他的 Eureka Server 节点的数据,故而设为 false。
    # eureka.client.serviceUrl.defaultZone:设置与 Eureka Server 交互的地址,查询服务和注册服务都需要依赖这个地址。默认是 http://localhost:8761/eureka ;多个地址可使用 , 分隔。
    server:
      port: 8764
    
    # 安全认证的配置
    security:
      basic:
        enabled: true
      user:
        name: chhliu  # 用户名
        password: chhliu123456   # 用户密码
    eureka:
      client:
        register-with-eureka: false
        fetch-registry: false
        service-url:
          defaultZone: http://chhliu:chhliu123456@localhost:8761/eureka  # 安全的注册地址
    

    启动方法

    package org.zhl.frame.zhlframe;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @SpringBootApplication
    @EnableEurekaServer
    public class ZhlFrameApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(ZhlFrameApplication.class, args);
    	}
    }
    
    4 条回复    2018-04-21 17:03:59 +08:00
    alvinbone88
        1
    alvinbone88  
       2018-04-21 00:29:01 +08:00   ❤️ 1
    把 trace 设为 true,然后把日志贴上来

    不过我猜的话,应该把 server.tomcat.max-http-post-size 设为一个较大的数
    maowu
        2
    maowu  
       2018-04-21 00:49:27 +08:00 via Android
    你这个用的是 Form 登陆方式吧?关掉其余只保留 basic 试试?
    yang608
        3
    yang608  
       2018-04-21 04:11:12 +08:00 via Android
    新版本的会自动开 csrf,关掉或者排除 URL 就行了
    Zovven
        4
    Zovven  
       2018-04-21 17:03:59 +08:00
    必须用 basic auth
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5298 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 01:26 · PVG 09:26 · LAX 17:26 · JFK 20:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.