我正在尝试在我的 sprin 启动应用程序中应用 auth0 安全性,但它会引发错误
Posted
技术标签:
【中文标题】我正在尝试在我的 sprin 启动应用程序中应用 auth0 安全性,但它会引发错误【英文标题】:i am trying to apply auth0 security in my sprin boot application but it throws an error 【发布时间】:2021-09-27 16:10:20 【问题描述】:这是我的 jwt 令牌提供者类:
@Component
public class JwtTokenProvider
@Value("$security.jwt.token.secret-key:secret-key")
private String secretKey;
@Value("$security.jwt.token.expire-length:3600000")
private long validityInMilliseconds = 3600000; // 1h
@Autowired
private MyUserDetails myUserDetails;
@PostConstruct
protected void init()
secretKey = Base64.getEncoder().encodeToString(secretKey.getBytes());
public String createToken(String username, List<Roles> roles)
Claims claims = Jwts.claims().setSubject(username);
claims.put("auth", roles.stream().map(s -> new SimpleGrantedAuthority(((GrantedAuthority) s).getAuthority()))
.filter(Objects::nonNull).collect(Collectors.toList()));
Date now = new Date();
Date validity = new Date(now.getTime() + validityInMilliseconds);
return Jwts.builder()//
.setClaims(claims)//
.setIssuedAt(now)//
.setExpiration(validity)//
.signWith(SignatureAlgorithm.HS256, secretKey)//
.compact();
public Authentication getAuthentication(String token)
UserDetails userDetails = myUserDetails.loadUserByUsername(getUsername(token));
return new UsernamePasswordAuthenticationToken(userDetails, "", userDetails.getAuthorities());
public String getUsername(String token)
return Jwts.parser().setSigningKey(secretKey).parseClaimsJws(token).getBody().getSubject();
public String resolveToken(HttpServletRequest req)
String bearerToken = req.getHeader("Authorization");
if (bearerToken != null && bearerToken.startsWith("Bearer "))
return bearerToken.substring(7);
return null;
public boolean validateToken(String token)
try
Jwts.parser().setSigningKey(secretKey).parseClaimsJws(token);
return true;
catch (JwtException | IllegalArgumentException e)
throw new CustomException("Expired or invalid JWT token", HttpStatus.INTERNAL_SERVER_ERROR);
这是我的 MyUserDetails 类:
public class MyUserDetails implements UserDetails
/**
*
*/
private static final long serialVersionUID = -8365671671277060768L;
private User user;
public MyUserDetails(User user)
this.user=user;
@Override
public Collection<? extends GrantedAuthority> getAuthorities()
Set<Roles> roles=user.getRoles();
List<SimpleGrantedAuthority> authorities = new ArrayList<>();
for(Roles role:roles)
authorities.add(new SimpleGrantedAuthority(role.getName()));
return authorities;
@Override
public String getPassword()
// TODO Auto-generated method stub
return user.getPassword();
@Override
public String getUsername()
// TODO Auto-generated method stub
return user.getUsername();
@Override
public boolean isAccountNonExpired()
// TODO Auto-generated method stub
return true;
@Override
public boolean isAccountNonLocked()
// TODO Auto-generated method stub
return true;
@Override
public boolean isCredentialsNonExpired()
// TODO Auto-generated method stub
return true;
@Override
public boolean isEnabled()
// TODO Auto-generated method stub
return user.isEnabled();
public UserDetails loadUserByUsername(String username)
// TODO Auto-generated method stub
return null;
这是我得到的错误:
说明:
com.yogi.security.jwtConfiguration.JwtTokenProvider 中的字段 myUserDetails 需要找不到类型为“securityapplied.MyUserDetails”的 bean。
注入点有如下注解:
@org.springframework.beans.factory.annotation.Autowired(required=true)
行动:
考虑在你的配置中定义一个“securityapplied.MyUserDetails”类型的bean。
【问题讨论】:
【参考方案1】:JwtTokenProvider 类正在尝试注入 MyUserDetails 类。 但是 MyUserDetails 类不能作为 bean 使用,因此会出现异常“考虑在配置中定义类型为 'securityapplied.MyUserDetails' 的 bean”。
尝试使用 @Component 注释 MyUserDetails 类,例如:
@Component
public class MyUserDetails implements UserDetails
【讨论】:
以上是关于我正在尝试在我的 sprin 启动应用程序中应用 auth0 安全性,但它会引发错误的主要内容,如果未能解决你的问题,请参考以下文章
无法使用 Flutter 1.22 在我的 XCODE 12.5.1 上启动我的应用程序