mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-18 03:01:05 +01:00
added authentication demo
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.api.rest.resources;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import javax.ws.rs.FormParam;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
@Singleton
|
||||
@Path("authentication")
|
||||
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
|
||||
public class AuthenticationResource
|
||||
{
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param request
|
||||
* @param username
|
||||
* @param password
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@POST
|
||||
public Response authenticate(@Context HttpServletRequest request,
|
||||
@FormParam("username") String username,
|
||||
@FormParam("password") String password)
|
||||
{
|
||||
Response response = null;
|
||||
|
||||
if ("hans".equals(username) && "hans123".equals(password))
|
||||
{
|
||||
request.getSession(true).setAttribute("auth", Boolean.TRUE);
|
||||
response = Response.ok().build();
|
||||
}
|
||||
else
|
||||
{
|
||||
response = Response.status(Response.Status.UNAUTHORIZED).build();
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param request
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GET
|
||||
public Response isAuthenticated(@Context HttpServletRequest request)
|
||||
{
|
||||
Response response = null;
|
||||
|
||||
if (request.getSession(true).getAttribute("auth") != null)
|
||||
{
|
||||
System.out.println( "authenticated" );
|
||||
|
||||
response = Response.ok().build();
|
||||
}
|
||||
else
|
||||
{
|
||||
response = Response.status(Response.Status.UNAUTHORIZED).build();
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user