【问题标题】:Could not resolve placeholder 'jwt.secret' in string value "${jwt.secret}"无法解析字符串值“${jwt.secret}”中的占位符“jwt.secret”
【发布时间】:2017-03-13 18:10:36
【问题描述】:

我已经使用注解配置了属性源:

@PropertySource(value = {"classpath:application.properties"}) 但我仍然收到错误提示 Could not resolve placeholder 'jwt.secret' in string value

@PropertySource(value = {"classpath:application.properties"})
public class JwtAuthenticationTokenFilter extends AbstractAuthenticationProcessingFilter {

    @Value("${jwt.header}")
    private String tokenHeader;

    public JwtAuthenticationTokenFilter() {
        super("/**");
    }
}

调度程序-servlet:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:security="http://www.springframework.org/schema/security"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/security 
        http://www.springframework.org/schema/security/spring-security-4.1.xsd
        http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:component-scan base-package="com.rsc."/>
    <mvc:annotation-driven />

    <mvc:resources mapping="/resources/**" location="/resources/"/>

    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>classpath:validation</value>
            </list>
        </property>
    </bean>

<!--    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:application.yml</value>
            </list>
        </property>
    </bean>-->

    <!--    <security:authentication-manager alias="authenticationManager">
        <security:authentication-provider user-service-ref="userDetailsServiceImpl">
            <security:password-encoder ref="encoder"></security:password-encoder>
        </security:authentication-provider>
    </security:authentication-manager>-->   

    <!--<bean id="userDetailsServiceImpl" class="com.rsc.service.UserDetailsServiceImpl"></bean>-->

    <bean id="JwtAuthenticationTokenFilter" class="com.rsc.security.JwtAuthenticationTokenFilter">
        <property name="authenticationManager" ref="authenticationManager" />
        <property name="authenticationSuccessHandler" ref="jwtAuthenticationSuccessHandler" />
    </bean>

    <security:authentication-manager alias="authenticationManager">
        <security:authentication-provider ref="jwtAuthenticationProvider" />
    </security:authentication-manager>

    <bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
        <constructor-arg name="strength" value="11"/>
    </bean>

    <!-- Configure the data source bean -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!-- Configure the entity manager factory bean -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="com.rsc.model"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hbm2ddl.auto">create</prop>
                <prop key="hibernate.id.new_generator_mappings">false</prop>
            </props>
        </property>
    </bean>

    <!-- Configure the transaction manager bean -->
    <bean id="transactionManager"
          class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <!-- Enable annotation driven transaction management -->
    <tx:annotation-driven/>

    <jpa:repositories base-package="com.rsc.repository"/>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

应用程序属性:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/rs_education
jdbc.username=admin
jdbc.password=admin

jwt:
  header: Authorization
  secret: my-very-secret-key
logging:
  level:
    org.springframework.security: DEBUG
server:
  port: 8888
spring:
  resources:
    chain:
      enabled: true
management:
  security:
    enabled: true # set to false to disable 'default' Spring Boot security

【问题讨论】:

    标签: java spring spring-mvc spring-security spring-boot


    【解决方案1】:

    如果你想使用 yaml,你的文件应该被命名为 application.yml,你可以使用这种格式。

    jwt:
      header: Authorization
      secret: my-very-secret-key
    

    如果您想使用属性样式格式,您的文件应命名为 application.properties 并使用以下格式:

    jwt.header=Authorization
    jwt.secret=my-very-secret-key
    

    【讨论】:

    • 你可以在 spring-boot 中为你的配置文件使用 YAML,所以你是不对的。
    • @KrzysztofAtłasik 是的,当然,我已经澄清了我的答案。
    【解决方案2】:

    如文档所述,您可以使用YAML,但您必须调用您的文件application.yml 而不是application.properties

    您还必须将snakeyaml 添加到您的依赖项中。

    【讨论】:

      【解决方案3】:

      我发现了一个新细节:如果你使用多模块,那么这个属性会写在mainyaml/properties中。不要使用模块的yaml/properties。无法扫描。

      【讨论】:

        【解决方案4】:

        应该在下面;

        @PropertySource(value = {"classpath:application.yml"})
        public class JwtAuthenticationTokenFilter extends AbstractAuthenticationProcessingFilter {
        
            @Value("${jwt.header}")
            private String tokenHeader;
        
            public JwtAuthenticationTokenFilter() {
                super("/**");
            }
        }
        

        固定注释。

        @PropertySource(value = {"classpath:application.yml"})
        

        【讨论】:

          猜你喜欢
          • 2017-07-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-04-24
          • 2018-06-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多