我正在尝试使用JWT令牌来保护REST API。为了保存编码的用户密码,我正在尝试使用bcrypt进行编码。但是,当我尝试自动装配BCryptPasswordEncoder时,我收到以下错误:
Could not autowire. No beans of BCryptPasswordEncoder type found. Check autowiring problems in bean class.
这就是我的控制器的样子:
package com.starter.springsecurity.demo.controller;
import com.starter.springsecurity.demo.domain.User;
import com.starter.springsecurity.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserRepository userRepository;
private BCryptPasswordEncoder bCryptPasswordEncoder;
public UserController(BCryptPasswordEncoder bCryptPasswordEncoder){
this.bCryptPasswordEncoder = bCryptPasswordEncoder;
}
@RequestMapping("/register")
public void register(@RequestBody User user){
}
}
这是我关注的博客: https://dzone.com/articles/implementing-jwt-authentication-on-spring-boot-api
Pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.starter.springsecurity</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Spring Security demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
有关Bcrypt的Spring Security 5有什么变化吗?
答案 0 :(得分:6)
您提到的文章进一步描述了它:
没有可以在UserController类中注入的BCryptPasswordEncoder的默认实例
稍后在代码中
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
您是否按照这些步骤定义了BCryptPasswordEncoder类?
答案 1 :(得分:0)
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
将此注入添加到同一类中,因为spring不能仅通过使用自动装配来自动注入此依赖项
答案 2 :(得分:0)
像这样在主应用程序类中添加它
static void Main(string[] args)
{
round();
Console.ReadKey();
}
public static Dates GetNearestRecord(DateTime dateTime, IEnumerable<Dates> records)
{
var nearestRecord = records.OrderBy(r => Math.Abs((r.Time - dateTime).TotalSeconds)).First();
return new Dates() { ID = nearestRecord.ID, Value = nearestRecord.Value, Time = dateTime };
}
public static IEnumerable<DateTime> GenerateIntervals(DateTime from, DateTime to, TimeSpan interval)
{
DateTime current = NearestInterval(from, interval);
DateTime last = NearestInterval(to, interval);
while (current <= last)
{
yield return current;
current = current.Add(interval);
}
}
public static DateTime NearestInterval(DateTime value, TimeSpan interval)
{
var temp = value.Add(new TimeSpan(interval.Ticks / 2));
var time = new TimeSpan((temp.TimeOfDay.Ticks / interval.Ticks) * interval.Ticks);
return value.Date.Add(time);
}
private static void round()
{
List<Dates> dates = Dates.GetDates();
var first = dates.Min(r => r.Time);
var last = dates.Max(r => r.Time);
var interval = TimeSpan.FromMinutes(15);
var intervals = GenerateIntervals(first, last, interval);
var nearestRecords = intervals.Select(i => GetNearestRecord(i, dates));
}
public class Dates
{
public int ID { get; set; }
public double Value { get; set; }
public DateTime Time { get; set; }
public static List<Dates> GetDates()
{
string input =
"| 1 | 31 | 2019-03-07 20:39:15 |\n" +
"| 2 | 12 | 2019-03-07 20:54:16 |\n" +
"| 5 | 33 | 2019-03-07 21:09:16 |\n" +
"| 7 | 11 | 2019-03-07 21:24:17 |\n" +
"| 8 | 9 | 2019-03-07 21:39:18 |\n" +
"| 7 | 14 | 2019-03-07 21:54:18 |\n" +
"| 8 | 15 | 2019-03-07 22:09:19 |\n" +
"| 7 | 16 | 2019-03-07 22:24:19 |\n" +
"| 8 | 31 | 2019-03-07 22:39:20 |\n" +
"| 7 | 3 | 2019-03-07 22:54:20 |\n" +
"| 8 | 34 | 2019-03-07 23:09:21 |\n" +
"| 7 | 10 | 2019-03-07 23:24:20 |\n" +
"| 8 | 17 | 2019-03-07 23:39:22 |\n" +
"| 7 | 18 | 2019-03-07 23:54:23";
List<Dates> dates = new List<Dates>();
string line = "";
StringReader reader = new StringReader(input);
while ((line = reader.ReadLine()) != null)
{
string[] lineArray = line.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
Dates newDate = new Dates()
{
ID = int.Parse(lineArray[0]),
Value = int.Parse(lineArray[1]),
Time = DateTime.Parse(lineArray[2])
};
dates.Add(newDate);
}
return dates;
}
}
答案 3 :(得分:0)
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
此代码适用于我