Spring-Security PasswordEncoder返回null

时间:2013-07-04 23:54:48

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

我在我的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>

2 个答案:

答案 0 :(得分:2)

你是否真的以某种方式将编码器注入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?

答案 1 :(得分:1)

@Autowired仅适用于Spring托管bean。因此,您的MyClass应为@Service@Repository@Component等(或者可能只是在您的XML中配置为<bean>

另外,我不明白你的

  

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

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