首页 > 解决方案 > JPARepository 搜索查询返回 null

问题描述

我无法理解为什么我的 userRepository 会返回 null,即使我的表中有类似的记录也是如此。我尝试用我的演示代码来做它并且它可以工作,但是当我尝试用用户身份验证来做它时它不起作用。

安全服务

@Path("/securityservice")
public class SecurityServices {

    private UserRepository userRepo;

//  http://localhost:8990/login/securityservice/security
    @GET
    @Path("security")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getOrderById(@QueryParam("orderId") int orderID,
            @HeaderParam("Authorization") String authString) throws JSONException {

        JSONObject json = new JSONObject();
        if (isUserAuthenticated(authString)) {
            json.put("INFO", "Authorized User!");
            return Response.status(200)
                    .entity(json.toString())
                    .type(MediaType.APPLICATION_JSON)
                    .build();

        } else {
            json.put("ERROR", "Unauthorized User!");
            return Response.status(403)
                    .entity(json.toString())
                    .type(MediaType.APPLICATION_JSON)
                    .build();
        }


    }
    private boolean isUserAuthenticated(String authString) {

            //authString = Basic 3hfjdksiwoeriounf
            String[] authParts = authString.split("\\s+");
            //authParts[0] = Basic
            //authParts[1] = 3hfjdksiwoeriounf
            String authInfo = authParts[1];
            byte[] bytes = Base64.getDecoder().decode(authInfo);
            String decodedAuth = new String(bytes);
            // decodedAuth = dj:1234
            String[] credentials = decodedAuth.split(":");
            //credentials[0]=dj
            //credentials[1]=1234
            System.out.println("HELLO"+credentials[0]);
            System.out.println("HELLO"+credentials[1]);
            User user = userRepo.findByUsername(credentials[0]); //this line returns null
            if (user != null) {
                return true;
            } else {
                return false;
            }

        }

用户类(JPA Repo 的 Getter 和 setter)

@Entity
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    @Column(name="firstname")
    private String firstName;
    @Column(name="lastname")
    private String lastName;
    private String password;
    private String username;
    @Column(name="accesstype")
    private String accessType;

    public User() {
        super();
    }

    public User(String firstName, String lastName, String password,
            String username, String accessType) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.password = password;
        this.username = username;
        this.accessType = accessType;
    }

    public long getId() {
        return id;
    }

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

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getAccessType() {
        return accessType;
    }

    public void setAccessType(String accessType) {
        this.accessType = accessType;
    }


}

标签: javaspringspring-data-jpajpql

解决方案


推荐阅读