如何使用Grails中的spring security rest插件进行身份验证
我使用的是Grails版本2.4.3。我正在创建一个支持RESTful API的应用程序。由于对这些API的访问应该经过身份验证,所以我尝试了SpringSecurityREST插件。我签出了,我能理解的是,如何使用Grails中的spring security rest插件进行身份验证,rest,grails,spring-security,grails-plugin,grails-2.4,Rest,Grails,Spring Security,Grails Plugin,Grails 2.4,我使用的是Grails版本2.4.3。我正在创建一个支持RESTful API的应用程序。由于对这些API的访问应该经过身份验证,所以我尝试了SpringSecurityREST插件。我签出了,我能理解的是,/api/login控制器是身份验证点,它以JSON格式接收用户凭据,并且在成功身份验证后,它提供acces令牌作为响应。我尝试用有效的JSON数据,使用。但它给了我以下的错误 401 Unauthorized , Similar to 403 Forbidden, but specific
/api/login
控制器是身份验证点,它以JSON格式接收用户凭据,并且在成功身份验证后,它提供acces令牌作为响应。我尝试用有效的JSON数据,使用。但它给了我以下的错误
401 Unauthorized , Similar to 403 Forbidden, but specifically for use when authentication is possible but has failed or not yet been provided. The response must include a WWW-Authenticate header field containing a challenge applicable to the requested resource.
我还尝试使用IntellijIDEA的REST客户端,但没有成功。
然后我尝试用有效的JSON数据向/api/login/
发送AJAX请求
,但在控制台上获得401。这里有什么问题?这是正确的登录终点吗?如何使用JQuery进行身份验证?您可以尝试使用此代码进行身份验证,我正在请求头中发送用户id和密码,您可以随意尝试:- 注入以下服务:-
def springSecurityService
def authenticationManager
并使用以下代码
def login = {
final String authorization = request.getHeader("Authorization");
if (authorization != null && authorization.startsWith("Basic")) {
boolean authResult = authenticateUser(authorization)
if (authResult) {
render response.status
} else {
render authFailed(response)
}
} else {
render authFailed(response)
}
}
protected boolean authenticateUser(String authorization) {
// Authorization: Basic base64credentials
def base64Credentials = authorization.substring("Basic".length()).trim();
byte[] credentials = base64Credentials.decodeBase64()
String actualCredential = new String(credentials)
// credentials format like username:password
final String[] values = actualCredential.split(":", 2);
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(values[0], values[1]);
try {
def authentication = authenticationManager.authenticate(authRequest);
def securityContext = SecurityContextHolder.getContext();
securityContext.setAuthentication(authentication);
def session = request.session;
session.setAttribute("SPRING_SECURITY_CONTEXT", securityContext);
}
catch (BadCredentialsException exception) {
return false
}
return true
}
protected HttpServletResponse authFailedResponse(HttpServletResponse response) {
response.setStatus(401)
response.setHeader("WWW-Authenticate", "Basic realm=\"nmrs_m7VKmomQ2YM3:\"")
return response;
}
试试这个
$.ajax({
url: " http://localhost:8080/AppName/api/login",
type: "POST",
crossDomain: true,
data: JSON.stringify({"username":"yourusername" , "password":"yourpassword"}),
contentType: 'application/json; charset=utf-8',
dataType: "json",
success: function (response) {
console.log(response);
},
error: function (xhr, status) {
alert("error");
}
}) });
谢谢但是我必须在哪里使用这个代码呢?
/api/login
是由插件创建的动态控制器吗?你能解释一下我如何使用这段代码吗?同样在同一个例子中,当我尝试使用usin时,它工作正常,我得到了预期的响应。。那么JQuery和REST客户端请求有什么问题吗?@Jrd您可以在方法中的控制器中使用它。若要使用POSTMAN发送JSON,请选择raw
tab->选择JSON并将JSON数据粘贴到文本区域我遇到类似问题。我的请求正确吗?POST/GhumVer3/api/login HTTP/1.1主机:本地主机:8080内容类型:应用程序/json缓存控制:无缓存邮递员令牌:02c8faa5-d1a9-9192-b26a-cd98186551f6{“用户名”:“测试”,“密码”:“测试123”}您尝试了什么以及如何尝试?请解释一下。。或者添加一些代码我在这里添加了代码: