java - Using Spring security 3 to authenticate against REST a user only with username -
this deal. have single authentication using username of application user, , have work angular, spring , mysql. already, have rest post service receive email (username) , have check if user exist in user table in database, , have response json structure:
{"response":{"loggedin":"true"}} or {"response":{"loggedin":"false"}}
i found example using http-basic authentication on spring security , works fine, issue approach not works me because approach needs , username , password, need username , check if user on database.
my question how can authentication spring security using rest service , single authentication user name field?
this 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>
this rest controller:
@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; }
}
thanks, useful help, , sorry english.
in spring security, can add custom authenticator. you'd need 1 ignores whatever comes in password , returns true. pass empty string password , it.
Comments
Post a Comment