【问题标题】:Spring-Security PasswordEncoder returns nullSpring-Security PasswordEncoder 返回 null
【发布时间】:2013-07-02 23:54:42
【问题描述】:

我已经在我的 struts2 应用程序上实现了 Spring 安全性,它运行良好,但它在第 3 行遇到错误 java.lang.NullPointerException。

虽然 passwordEncoder 配置似乎是通过添加那些我无法再使用纯文本密码登录的配置来工作的。

 <authentication-manager> 
        <authentication-provider>
           <password-encoder ref="passwordEncoder"/>

            <jdbc-user-service data-source-ref="dataSource"

                           users-by-username-query="
              select username,password,enabled 
              from Users where username=?" 

                           authorities-by-username-query="
                      select username,authority 
                      from Users where username = ?"

        />
        </authentication-provider> 

    </authentication-manager> 
    <beans:bean id="passwordEncoder" 
        xmlns="http://www.springframework.org/schema/beans"            
        class="org.springframework.security.authentication.encoding.ShaPasswordEncoder">
        <constructor-arg value="256"/>
    </beans:bean>
</beans:beans>

MyClass.java

    import org.springframework.security.authentication.encoding.ShaPasswordEncoder;;
    ....
    private ShaPasswordEncoder passwordEncoder;

     public ShaPasswordEncoder getPasswordEncoder() {
         return passwordEncoder;
     }
     @Autowired
     public void setPasswordEncoder(ShaPasswordEncoder passwordEncoder) {
           this.passwordEncoder = passwordEncoder;
      }

     public void encode(String username)
        {
1             System.err.println("encode password");
2             String encodedPassword = "";
3             encodedPassword = passwordEncoder.encodePassword("Jack",username);
4             System.err.println("encoded password " + encodedPassword);
        }

pom.xml

   <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>

【问题讨论】:

    标签: jakarta-ee struts2 spring-security password-encryption


    【解决方案1】:

    您是否真的以某种方式将编码器注入到 Encode.java 中?如果没有,那是你的问题。

    如果您需要一个可能会起作用的快速而肮脏的解决方案。做吧

     private ShaPasswordEncoder passwordEncoder=new ShaPasswordEncoder(256);
    

    然后找出你在注射时做错了什么。

    也许可以阅读这篇文章来了解应该如何进行注入。

    What is the difference between @Inject and @Autowired in Spring Framework? Which one to use under what condition?

    【讨论】:

    • 不,我该怎么办?我不知道,在教程中找不到任何东西
    • 使用注释或 xml 将名为“passwordEncoder”的 bean 实例注入到您的 Encode.java。阅读 spring 手册,了解如何注入东西。
    • @JackRamzi @Autowired 被放置在 setter 方法或字段属性上。
    • @RomanC 我已经把它放在了 setter 上,但仍然遇到同样的错误
    • 您是否启用了&lt;context:annotation-config /&gt;?否则@Autowired 将不会被解释。
    【解决方案2】:

    @Autowired 仅适用于 Spring 托管 bean。所以你的MyClass 应该是@Service@Repository@Component 等(或者它可以简单地在你的XML 中配置为&lt;bean&gt;

    另外,我不明白你的意思

    我不能再用纯文本密码登录了。

    用户输入密码的编码以及与数据库中编码密码的比较是由spring security在内部完成的。它所需要的只是一个UserDetails 对象,该对象将由您的jdbc-user-service 提供。

    【讨论】:

    • 我使用了@Service 但还是有同样的问题!
    猜你喜欢
    • 2020-10-27
    • 2018-09-14
    • 2021-08-30
    • 2016-07-24
    • 1970-01-01
    • 2013-07-08
    • 2016-11-28
    • 1970-01-01
    相关资源
    最近更新 更多