Im building a web service app using jersey. For authorization/authentication im using apache shiro.
I found some tutorials showing how to use apache shiro in a web app. They show the login method using a .jsp page that have a username and password field and than this .jsp page is configured in shiro.ini like this:
[main]
shiro.loginUrl = /login.jsp
[urls]
/login.jsp = authc
/logout = logout
I Wanna know how to make this authentication without a any .jsp page, because my project have only web services. So i think that i need a login service, than i created one:
@POST
@Path("/login")
public Response login(@FormParam("username") final String username, @FormParam("password") final String password, @FormParam("remember") final boolean remember) {
final Subject currentUser = SecurityUtils.getSubject();
if (!currentUser.isAuthenticated()) {
final UsernamePasswordToken token = new UsernamePasswordToken(username, password);
try {
token.setRememberMe(remember);
currentUser.login(token);
} catch (final AuthenticationException e) {
return Response.status(Status.BAD_REQUEST).entity("Usuário ou senha inválido").build();
}
}
And this is my shiro.ini conf:
[urls]
/security/login = anon
/security/isAuthenticated = anon
/** = authcBasic
Once that the user wont be authenticated to log in i include /security/login = anon.
Is this the correct way to authenticated a user with apache shiro in a webservice environment?