Spring boot security password not printed

4.7k views Asked by At

I have a basic spring boot RESTful application which works fine but when I try to add authorization using spring-boot-starter-security the password is not printed on the console. Following are the contents of the relevant files

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>

controller class

package com.be.test.api;

@RestController
public class TestController {

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

Main class

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);

    }
}

Is there anything missing from any of the files ?

2

There are 2 answers

0
Dung Phan On

You add @EnableAutoConfiguration to file TestNameApplication

@EnableWebSecurity

public class TestNameApplication {

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

}

You can run file TestNameApplication, then you can see pass in console log IDE. Spring will auto generate password belong to format:

Using default security password: a72f8e06-d1ac-4211-a84d-933534359c52

0
Joel On

Add

logging.level.org.springframework.security=INFO

to your application.properties file. This came from the first note on this page, and an example of it is here.