【问题标题】:Spring: Security-context doesn't save my passwordSpring:安全上下文不保存我的密码
【发布时间】:2015-12-01 16:44:36
【问题描述】:

我正在编写带有基本身份验证的其余应用程序。它工作正常,但每次重新启动后都会生成新的用户密码。 我的 Security-context.xml 是

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:security="http://www.springframework.org/schema/security"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/security

           http://www.springframework.org/schema/security/spring-security-3.0.xsd">

    <security:http auto-config='true'>
        <security:intercept-url pattern="/api/**" access="ROLE_USER"/>
        <security:intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
        <security:http-basic />
    </security:http>

    <security:authentication-manager>
        <security:authentication-provider>
            <security:user-service>
                <security:user name="user" password="password1" authorities="ROLE_USER" />
            </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager>

</beans>

为什么不用password="password1"?

【问题讨论】:

  • 您能解释一下“它会生成新的用户密码”是什么意思吗?你在哪里看到这个?另外,请在提交身份验证请求时发布调试日志。
  • 在控制台中,当我的应用启动类似“使用默认安全密码:c80fe464-e7e8-4t1e-8cea-123f26d702c3”时
  • 如果您搜索“使用默认安全密码”这个短语,它似乎是某种创建默认“用户”的 Sprint Boot 功能。与它相关的各种问题on github。确保您使用的是最新版本的所有内容,并尝试使用不会与默认用户名冲突的其他用户名。
  • @LukeTaylor 检查并尝试过,没有任何帮助(((((
  • Spring Boot 默认使用生成的密码创建默认用户。基本上你的 xml 什么都不做,可以(或应该)被删除。如果您想自己指定用户,请按照 Spring Boot 参考指南中的说明配置 Spring Security。

标签: java spring spring-security spring-boot


【解决方案1】:

您可以在application.properties文件中设置用户和密码,如下所示。

security.user.name=user # Default user name.
security.user.password= # Password for the default user name. A random password is logged on startup by default.

这直接来自 Spring Boot 属性文档Here

除非你有充分的理由这样做,否则不要与 Spring Boot 的自动配置魔法抗衡。

【讨论】:

    猜你喜欢
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    相关资源
    最近更新 更多