Spring-Security PasswordEncoder返回null [英] Spring-Security PasswordEncoder returns null

查看:103
本文介绍了Spring-Security PasswordEncoder返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的struts2应用程序上实现了Spring安全性,并且可以完美运行,但是在第3行遇到了java.lang.NullPointerException错误.

I have implemented Spring security on my struts2 application and it perfectly works, but it runs into error java.lang.NullPointerException on line 3.

尽管看来passwordEncoder配置的工作方式是添加了我无法再使用纯文本密码登录的那些密码.

Although it seems passwordEncoder configuration works as by adding those I cant login with a plain text password any more.

 <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>

推荐答案

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

Do you actually inject the encoder into Encode.java somehow somewhere? If not,then that is your problem.

如果您需要一个快速有效的解决方案,它可能会起作用.做到

if you need a quick and dirty solution which will probably work. just make it

 private ShaPasswordEncoder passwordEncoder=new ShaPasswordEncoder(256);

,然后找出您在注射中做错了什么.

and then find out what you are doing wrong with the injection.

可以阅读此书以了解应如何进行注射.

maybe read this to see how injection should be done.

在Spring Framework中@Inject和@Autowired?在什么条件下使用哪个?

这篇关于Spring-Security PasswordEncoder返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆