当前位置: 首页 > 知识库问答 >
问题:

通过字段“User TokenService”表示的不满足的依赖项

谢裕
2023-03-14
    package com.book.controller;

import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.book.entity.User;
import com.book.entity.security.UserToken;
import com.book.entity.security.service.SecurityService;
import com.book.entity.security.service.UserTokenService;

@Controller
public class HomeController {
    @Autowired
    private UserTokenService userTokenService;
    @Autowired
    private SecurityService securityService;

    @RequestMapping("/")
    public String getHome() {
        return "index";
    }

    @RequestMapping("/myaccount")
    public String myAccount() {
        return "myAccount";
    }

    @RequestMapping("/login")
    public String login(Model model) {
        model.addAttribute("classActiveLogin", true);
        return "myAccount";
    }

    @RequestMapping("/forgetPassword")
    public String forgetPassword(Model model) {
        model.addAttribute("classActiveForgetPassword", true);
        return "myAccount";
    }

    @RequestMapping("/newUser")
    public String newUser(Locale locale, @RequestParam("token") String token, Model model) {

        UserToken userToken = userTokenService.getPasswordResetToken(token);
        if (userToken == null) {
            String msg = "Invalid Token";
            model.addAttribute("msg", msg);
            return "redirect:/badRequest";
        }

        User user = userToken.getUser();
        String username = user.getUsername();

        UserDetails userDetails = securityService.loadUserByUsername(username);

        Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(),
                userDetails.getAuthorities());

        SecurityContextHolder.getContext().setAuthentication(authentication);

        model.addAttribute("classActiveEdit", true);
        return "myProfile";
    }
}
package com.book.entity.security;

import java.util.Calendar;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

import com.book.entity.User;

@Entity
public class UserToken {

    private static final int EXPIRATION = 60 * 24;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String token;

    @OneToOne(targetEntity = User.class, fetch = FetchType.EAGER)
    @JoinColumn(nullable=false, name="user_id")
    private User user;

    private Date expiryDate;

    public UserToken(final String token, final User user) {
        super ();

        this.token = token;
        this.user = user;
        this.expiryDate = calculateExpiryDate(EXPIRATION);
    }

    private Date calculateExpiryDate (final int expiryTimeInMinutes) {
        final Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(new Date().getTime());
        cal.add(Calendar.MINUTE, expiryTimeInMinutes);
        return new Date(cal.getTime().getTime());
    }

    public void updateToken(final String token) {
        this.token = token;
        this.expiryDate = calculateExpiryDate(EXPIRATION);
    }

    @Override
    public String toString() {
        return "P_Token [id=" + id + ", token=" + token + ", user=" + user + ", expiryDate=" + expiryDate + "]";
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Date getExpiryDate() {
        return expiryDate;
    }

    public void setExpiryDate(Date expiryDate) {
        this.expiryDate = expiryDate;
    }

    public static int getExpiration() {
        return EXPIRATION;
    }



}
package com.book.entity.security.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.book.entity.User;
import com.book.entity.security.UserToken;
import com.book.entity.security.repo.PasswordResetRepo;
import com.book.entity.security.repo.UserTokenRepo;

@Service("userTokenService")
public class UserTokenService implements UserTokenRepo{

    @Autowired
    private PasswordResetRepo repo;

    @Override
    public UserToken getPasswordResetToken(final String token) {
        return repo.findByToken(token);
    }

    @Override
    public void createPasswordResetTokenForUser(final User user, final String token) {
        final UserToken myToken = new UserToken(token, user);
        repo.save(myToken);
    }
}
package com.book.entity.security.repo;

import com.book.entity.User;
import com.book.entity.security.UserToken;

public interface UserTokenRepo {

    UserToken getPasswordResetToken(final String token);

    void createPasswordResetTokenForUser(final User user, final String token);

}
package com.book.entity.security.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import com.book.entity.User;
import com.book.entity.security.repo.SecurityUserRepository;

@Service
public class SecurityService implements UserDetailsService {
    @Autowired
    private SecurityUserRepository securityUserRepository;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = securityUserRepository.findByUsername(username);

        if (null == user) {
            throw new UsernameNotFoundException("Username not found");
        }

        return user;
    }
}
package com.book.entity.security.repo;

import java.util.Date;
import java.util.stream.Stream;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import com.book.entity.User;
import com.book.entity.security.UserToken;


public interface PasswordResetRepo extends JpaRepository<UserToken, Long > {

    UserToken findByToken(String token);

    UserToken findByUser(User user);

    Stream<UserToken> findAllByExpiryDateLessThan(Date now);

    @Modifying
    @Query("delete from P_Token t where t.expirydate <= ?1")
    void deleteAllExpiredSince(Date now);
}

错误:-->org.springframework.beans.factory.unsatisfieddependencyexception:创建名为“home controller”的bean时出错:通过字段“user tokenservice”表示的不满足的依赖项;嵌套异常为org.springframework.beans.factory.unsatisfieddependencyexception:创建名为“user tokenservice”的bean时出错:通过字段“repo”表示的不满足的依赖关系;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“password resetrepo”的bean时出错:调用init方法失败;嵌套异常为java.lang.NoClassDefoundError:antlr/recognitionException

共有1个答案

谢阳成
2023-03-14

问题可能出在HomeController上:

@Autowired
    private UserTokenService userTokenService;

将此代码替换为贝娄代码:

@Autowired
        private UserTokenRepo userTokenService;

您的主要错误部分是:

<dependency>
 <groupId>org.antlr</groupId>
 <artifactId>antlr-complete</artifactId>
 <version>3.5.2</version>
</dependency>  

请检查passwordresetrepo存储库上所有JPA查询。有时,如果查询与实体变量名不匹配,那么Spring就不能为存储库创建bean

希望这能解决你的问题。

谢谢:)

 类似资料: