简体   繁体   English

如何在Spring启动应用程序中将用户添加到嵌入式tomcat?

[英]How to add a user to an embedded tomcat in a spring boot application?

I am trying to create a spring boot application, everything runs fine except when I have to insert the username and password when I access localhost:8080. 我正在尝试创建一个Spring启动应用程序,一切运行正常,除非我必须在访问localhost时插入用户名和密码:8080。 I don't know how to add a new tomcat user to the embedded tomcat from my app. 我不知道如何从我的应用程序添加新的tomcat用户到嵌入式tomcat。

This is my pom xml: 这是我的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>com.mastercrafters</groupId>
    <artifactId>mastercrafters</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>mastercrafters</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.BUILD-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <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>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

And here is the structure of my project where tomcat is visible: 这是我的项目的结构,其中tomcat是可见的:

嵌入式tomcat文件夹

从localhost登录

How can I disable the login from application.properties if possible or what solution is for this problem ? 如果可能,如何禁用application.properties的登录或者针对此问题的解决方案是什么? Many thanks in advance ! 提前谢谢了 !

You have Spring Security as a dependency. 您将Spring Security作为依赖项。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

If Spring security is on the classpath and no security config is made in your application, Spring will configure basic authentication. 如果Spring安全性在类路径上并且您的应用程序中没有安全配置,则Spring将配置基本身份验证。 This is a fail safe. 这是一个失败的安全措施。 You can find the generated username and password in your console. 您可以在控制台中找到生成的用户名和密码。

You have two options: 您有两种选择:

  • If you don't want a login system, remove the spring security dependency 如果您不想使用登录系统,请删除spring security依赖项
  • If you do want security, you need to configure spring security so that spring knows what authentication mechanism is used (basic, form, oauth, etc.) and how to authenticate users. 如果确实需要安全性,则需要配置spring security,以便spring知道使用的身份验证机制(基本,表单,oauth等)以及如何对用户进行身份验证。

您需要阅读有关默认Spring Security Filter Chain如何工作的更多信息 - 您不需要Tomcat用户(就像您想要访问Tomcat Manager应用程序一样) - 您需要允许Anonymous Auth或其他形式的Autnetication它将拒绝任何对该服务的请求。

You can refer below link to configure Embedded tomcat through application.properties file only. 您可以参考下面的链接,仅通过application.properties文件配置嵌入式tomcat。 You can also use external tomcat where you can give tomcat-users.xml as before. 您也可以使用外部tomcat,您可以像以前一样提供tomcat-users.xml。

server.tomcat.basedir= # Tomcat base directory. If not specified a temporary directory will be used.

http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file

In spring boot 2.x , you can use the default security settings (default user and password printed to the console), or can implement your own UserDetailsService , as described in spring-boot documentation here and here ; spring boot 2.x中 ,您可以使用默认的安全设置(打印到控制台的默认用户和密码),或者可以实现您自己的UserDetailsS​​ervice ,如此此处的 spring-boot文档中所述;

This is a detailed example on how to dynamically set your own user/password: stackoverflow link 这是关于如何动态设置自己的用户/密码的详细示例: stackoverflow链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM