【问题标题】:Spring boot security password not printedSpring Boot 安全密码未打印
【发布时间】:2017-05-17 05:51:08
【问题描述】:

我有一个基本的 spring boot RESTful 应用程序,它工作正常,但是当我尝试使用 spring-boot-starter-security 添加授权时,密码不会打印在控制台上。以下是相关文件的内容

application.properties

logging.file=route.log
logging.level.org.springframework.web=ERROR
logging.level.org.springframework.boot.test=ERROR
logging.level.org.springframework.boot.autoconfigure.security=INFO

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         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>com.be.test</groupId>
    <artifactId>testName</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.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>1.8</java.version>
        <start-class>com.be.test.TestNameApplication</start-class>
    </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.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.google.maps/google-maps-services -->
        <dependency>
            <groupId>com.google.maps</groupId>
            <artifactId>google-maps-services</artifactId>
            <version>0.1.17</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

控制器类

package com.be.test.api;

@RestController
public class TestController {

    @RequestMapping(value = "/test", method=RequestMethod.GET)
    public
    @ResponseBody
    long test() {
        return 1000;
    }
}

主类

package com.be.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication()
@ComponentScan
@EnableAutoConfiguration
public class TestNameApplication {

    public static void main(String[] args) throws Exception {

        ApplicationContext ctx = SpringApplication.run(TestNameApplication.class, args);

    }
}

文件中是否缺少任何内容?

【问题讨论】:

  • 我在您的代码中没有看到打印密码的语句。打印一个秘密密码会有用吗? ;-)
  • @JimHawkins Spring Boot 通常会自动执行此操作。 BlueEagle:使用 application.properties 设置密码怎么样?
  • @Marged 我试过了,但还是不行。我在没有任何用户名/密码的情况下发送请求(curl 和 postman),它运行成功。

标签: java spring rest maven restful-authentication


【解决方案1】:

添加

logging.level.org.springframework.security=INFO

到您的application.properties 文件。这来自this page 上的第一个注释,例如here

【讨论】:

    【解决方案2】:

    您将@EnableAutoConfiguration 添加到文件TestNameApplication

    @EnableWebSecurity

    公共类 TestNameApplication {

    public static void main(String[] args) {
        ApplicationContext ctx =  SpringApplication.run(TestNameApplication .class, args);
    }
    

    }

    您可以运行文件 TestNameApplication,然后您可以看到通过控制台日志 IDE。 Spring会自动生成密码属于格式:

    使用默认安全密码:a72f8e06-d1ac-4211-a84d-933534359c52

    【讨论】:

      猜你喜欢
      • 2016-04-14
      • 2014-02-23
      • 2019-12-16
      • 2015-08-25
      • 1970-01-01
      • 1970-01-01
      • 2012-11-29
      • 1970-01-01
      相关资源
      最近更新 更多