即使在表中有类似记录的情况下,我也无法理解为什么userRepository返回null的原因。我尝试使用演示代码来完成此操作,但是它可以工作,但是当我尝试使用用户身份验证时,它不起作用。
安全服务
string strUrl = "http://site-2016-02:1000";
using (SPSite oSite = new SPSite(strUrl))
{
using (SPWeb oWeb = oSite.RootWeb.Webs["PWA"])
用户类(JPA存储库的字母和字母设置器)
@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;
}
}