Java 使用SpringSecurity3仅使用用户名对REST用户进行身份验证
这就是交易。我必须只使用应用程序用户的用户名进行单一身份验证,并且必须使用angular、spring和mysql。我已经有了一个REST POST服务,可以接收电子邮件(用户名),我必须检查该用户是否存在于数据库的用户表中,并且我必须使用以下JSON结构进行响应:Java 使用SpringSecurity3仅使用用户名对REST用户进行身份验证,java,spring,rest,authentication,spring-security,Java,Spring,Rest,Authentication,Spring Security,这就是交易。我必须只使用应用程序用户的用户名进行单一身份验证,并且必须使用angular、spring和mysql。我已经有了一个REST POST服务,可以接收电子邮件(用户名),我必须检查该用户是否存在于数据库的用户表中,并且我必须使用以下JSON结构进行响应: {"response":{"loggedIn":"true"}} or {"response":{"loggedIn":"false"}} 我在spring security上找到了一个使用http basic身份验证的示例,效果
{"response":{"loggedIn":"true"}} or {"response":{"loggedIn":"false"}}
我在spring security上找到了一个使用http basic身份验证的示例,效果很好,问题是该方法对我不起作用,因为该方法需要用户名和密码,但我只需要用户名并检查该用户是否在数据库中
我的问题是如何使用REST服务和仅使用用户名字段的单一身份验证来使用spring security进行身份验证
这是我的spring-context.xml
<security:http pattern="/login" security="none">
</security:http>
<security:http auto-config="true">
<security:intercept-url pattern="/**" access="ROLE_REST" />
<security:http-basic />
<security:logout logout-url="/logout" logout-success-url="/" invalidate-session="true" delete-cookies="true" />
</security:http>
<security:authentication-manager>
<security:authentication-provider user-service-ref="watUserDetailService">
<security:password-encoder hash="md5" />
</security:authentication-provider>
</security:authentication-manager>
}
谢谢您的帮助,我的英语也很抱歉。在Spring Security中,您可以添加自定义验证器。您需要一个简单地忽略密码中的任何内容并返回true的密码。然后你传递一个空字符串作为密码,就这样了
@Controller
public class UserController {
@Autowired
ILoginService loginService;
@RequestMapping(value = "/login", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
public @ResponseBody IResponse login(@RequestBody @Valid LoginForm form, BindingResult result) {
IResponse loginReponse = null;
if (result.hasErrors()) {
ResponseError errorReponse = new ResponseError();
errorReponse.getError().put("key", KeyConstants.NOT_VALID_EMAIL_ERROR_KEY );
loginReponse = errorReponse;
} else {
Response response = new Response();
User loggedUser = null;
try {
loggedUser = loginService.login(form.getEmail());
if (loggedUser != null) {
response.getResponse().put("loggedIn", Boolean.TRUE.toString());
}
loginReponse = response;
} catch (ServiceException e) {
response.getResponse().put("loggedIn", Boolean.FALSE.toString());
loginReponse = response;
}
}
return loginReponse;
}