gpt4 book ai didi

jakarta-ee - Spring-Security PasswordEncoder 返回 null

转载 作者:行者123 更新时间:2023-12-02 17:43:41 24 4
gpt4 key购买 nike

我已经在我的 struts2 应用程序上实现了 Spring security,它运行良好,但它在第 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>

最佳答案

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

如果您需要一个可能会奏效的快速而肮脏的解决方案。就这样吧

 private ShaPasswordEncoder passwordEncoder=new ShaPasswordEncoder(256);

然后找出你在注入(inject)时做错了什么。

也许阅读本文以了解应该如何进行注入(inject)。

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

关于jakarta-ee - Spring-Security PasswordEncoder 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17479387/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com