我正在与springboot和springsecurity合作构建一个后端应用程序,我正在将用户存储到cloudfirestore非关系数据库中,我正在使用google在firebase平台上提供的admin-sdk令牌。我正在按以下方式初始化firestore。
@Service
public class UserFirestoreInitialize {
@Value("classpath:static/gamingplatform-c922d-firebase-adminsdk-c25o8-06e92edfd5.json")
Resource resourceFile;
@PostConstruct
public void initialize() {
try {
InputStream serviceAccount = resourceFile.getInputStream();
GoogleCredentials cred = GoogleCredentials.fromStream(serviceAccount)
.createScoped("https://www.googleapis.com/auth/datastore");
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(cred)
.setDatabaseUrl("FIREBASE_URL")
.build();
FirebaseApp.initializeApp(options);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
我还有一个像这样的配置类。
@Configuration
public class UserFirestoreConfiguration{
@Bean
public Firestore getFirestore(){
return FirestoreClient.getFirestore();
}
}
在此之后,我可以很容易地在我的userservice中使用这个bean,如下所示:
@Service
public class UserService{
@Autowired
private Firestore firestore;
public User getUser(){
//Query for user.
}
//Post, Put, delete
}
这在某种程度上起了作用。当我将Spring Security 添加到应用程序中时,问题就出现了。当我运行maven安装时,应用程序没有生成,问题如下:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jwtTokenFilter': Unsatisfied dependency expressed through field 'userDetailsService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceDetailsImpl' defined in file [/Users/igorzelaya/SoftwareDev/D1Gaming-User-Back-end1/target/classes/com/d1gaming/user/security/UserServiceDetailsImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' defined in file [/Users/igorzelaya/SoftwareDev/D1Gaming-User-Back-end1/target/classes/com/d1gaming/user/user/UserService.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.d1gaming.user.user.UserService]: Constructor threw exception; nested exception is java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist.
我尝试隔离这个问题,我创建了另一个项目,并尝试连接到我的数据库,就像我在上面的代码片段中所做的^,一切正常,然后我一个接一个地复制我的配置类,我想我的应用程序开始崩溃的那一刻,我添加了我的jwt令牌过滤器类,这个类看起来像这样
@Component
public class JwtTokenFilter extends OncePerRequestFilter{
@Autowired
private JwtTokenUtil jwtTokenUtil;
@Autowired
private UserServiceDetailsImpl userDetailsService;
@Override
//Get authorization header and validate it.
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)throws ServletException, IOException, NullPointerException {
try {
String jwt = parseJwt(request);
if(jwt != null && jwtTokenUtil.validate(jwt)) {
String username = jwtTokenUtil.getUserNameFromJwtToken(jwt);
UserDetails userDetails = userDetailsService.loadUserByUsername(username);
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails,null,userDetails.getAuthorities());
authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
SecurityContextHolder.getContext().setAuthentication(authentication);
}
}
catch(Exception e) {
logger.error("Cannot set user authentication: {}", e);
}
chain.doFilter(request, response);
}
private String parseJwt(HttpServletRequest request) {
String headerAuth = request.getHeader("Authorization");
if(StringUtils.hasText(headerAuth) && headerAuth.startsWith("Bearer ")) {
return headerAuth.substring(7, headerAuth.length());
}
return null;
}
}
正如您在上面显示的我的堆栈跟踪中所注意到的,错误开始于注入此类、jwttokenutil类和userdetailsimpl中的依赖项jwttokenutil类如下:
@Component
public class JwtTokenUtil {
@Value("${app.jwtSecret}")
private String jwtSecret;
@Value("${app.jwtExpirationMs}")
private int jwtExpirationMs;
private final Logger logger = LoggerFactory.getLogger(JwtTokenUtil.class);
public String generateJwtToken(Authentication authentication) {
UserDetailsImpl userPrincipal = (UserDetailsImpl) authentication.getPrincipal();
return Jwts.builder()
.setSubject(userPrincipal.getUsername())
.setIssuedAt(new Date())
.setExpiration(new Date((new Date()).getTime() + jwtExpirationMs))
.signWith(SignatureAlgorithm.HS512, jwtSecret)
.compact();
}
public String getUserNameFromJwtToken(String token) {
return Jwts.parser().setSigningKey(jwtSecret)
.parseClaimsJws(token).getBody().getSubject();
}
public String getUserId(String token) {
Claims claims = Jwts.parser()
.setSigningKey(jwtSecret)
.parseClaimsJws(token)
.getBody();
return claims.getSubject().split(",")[0];
}
public String getUsername(String token) {
Claims claims = Jwts.parser()
.setSigningKey(jwtSecret)
.parseClaimsJws(token)
.getBody();
return claims.getSubject().split(",")[0];
}
public Date getExpirationDate(String token) {
Claims claims = Jwts.parser()
.setSigningKey(jwtSecret)
.parseClaimsJws(token)
.getBody();
return claims.getExpiration();
}
public boolean validate(String token) {
try {
Jwts.parser()
.setSigningKey(jwtSecret).parseClaimsJws(token);
return true;
}
catch(SignatureException e) {
logger.error("Invalid JWT signature - {}",e.getMessage());
}
catch(MalformedJwtException e) {
logger.error("Invalid JWT token - {}", e.getMessage());
}
catch(ExpiredJwtException e) {
logger.error("Invalid JWT token - {}",e.getMessage());
}
catch(UnsupportedJwtException e) {
logger.error("Invalid JWT token - {}", e.getMessage());
}
catch(IllegalArgumentException e) {
logger.error("Invalid JWT token - {}", e.getMessage());
}
return false;
}
}
userservicedetailsimpl类:
public class UserDetailsImpl implements UserDetails{
private static final long serialVersionUID = 1L;
private String userId;
private String userRealName;
private String userName;
@JsonIgnore
private String userPassword;
private String userEmail;
private UserStatus userStatusCode;
private Team userTeam;
private Map<String,Object> userBilling;
private String userCountry;
private int userTokens;
private double userCash;
private Map<String, Object> userBirthDate;
private Collection<? extends GrantedAuthority> authorities;
public UserDetailsImpl(String userId, String userRealName, String userName, String userPassword, String userEmail,
UserStatus userStatusCode, Team userTeam, Map<String, Object> userBilling, String userCountry,
int userTokens, double userCash, Map<String, Object> userBirthDate,
Collection<? extends GrantedAuthority> authorities) {
this.userId = userId;
this.userRealName = userRealName;
this.userName = userName;
this.userPassword = userPassword;
this.userEmail = userEmail;
this.userStatusCode = userStatusCode;
this.userTeam = userTeam;
this.userBilling = userBilling;
this.userCountry = userCountry;
this.userTokens = userTokens;
this.userCash = userCash;
this.userBirthDate = userBirthDate;
this.authorities = authorities;
}
public static UserDetailsImpl build(User user) {
List<GrantedAuthority> authorities = user.getUserRoles().stream()
.map(role -> new SimpleGrantedAuthority(role.getRoleType().name()))
.collect(Collectors.toList());
return new UserDetailsImpl(user.getUserId(),user.getUserRealName(),user.getUserName(),user.getUserPassword(),user.getUserEmail()
,user.getStatusCode(),user.getUserTeam(),user.getUserBilling(),user.getUserCountry(),
user.getUserTokens(),user.getUserCash(),user.getUserBirthDate(),authorities
);
}
//Getters and setters
}
如您所见,这两个类都与firestore无关,目前我非常困惑,所以我做了以下操作。我注意到由于某种奇怪的原因,@postconstruct注解不起作用,spring没有在第一段代码上初始化initializefirestore()方法,因此我直接在spring boot上放置了initialize()方法,如下所示:
@SpringBootApplication
public class UserApplication(){
public static void main(String[] args) {
//Basically does the same as the initialize method mentioned before.
UserFirestoreUtils.initialize(UserFirestoreUtils.getOptions());
SpringApplication.run(D1GamingUserBackEnd1Application.class, args);
}
}
有趣的是,当我调试这个时,出现了以下错误:
The dependencies of some of the beans in the application context form a cycle:
jwtTokenFilter (field private com.d1gaming.user.security.UserServiceDetailsImpl com.d1gaming.user.security.JwtTokenFilter.userDetailsService)
┌─────┐
| userServiceDetailsImpl defined in file [/Users/igorzelaya/SoftwareDev/D1Gaming-User-Back-end1/target/classes/com/d1gaming/user/security/UserServiceDetailsImpl.class]
↑ ↓
| userService (field private org.springframework.security.crypto.password.PasswordEncoder com.d1gaming.user.user.UserService.passwordEncoder)
↑ ↓
| userSecurityConfiguration (field com.d1gaming.user.security.UserServiceDetailsImpl com.d1gaming.user.security.UserSecurityConfiguration.userDetailsService)
└─────┘
奇怪的是,当我在它上运行maven安装时,堆栈跟踪与我在问题开始时显示的一样,我觉得云firestore上没有足够的文档,而且考虑到它是“新的”,但这仍然是恼人的,因为我找不到“正确的方法”这样做,谷歌文档显然是不够的。我很抱歉,如果我包括太多的代码,我认为这是必要的,但无论如何感谢您的时间,我感谢如果有人可以帮助我。祝您有个美好的一天。
1条答案
按热度按时间ht4b089n1#
这看起来像是spring依赖项和firebase管理之间的冲突。您可以尝试使用firestore客户端库。firebase管理sdk是一个更广泛的库,其中一些代码可能会与spring security发生冲突