Web Services Basic Security-RAGHU
Web Services Basic Security-RAGHU
Web Services Basic Security-RAGHU
Page 1 of 5
by RAGHU SIR[NARESH IT, HYD]
Filter code:
package in.nit.validator;
import java.io.IOException;
Page 2 of 5
by RAGHU SIR[NARESH IT, HYD]
import java.util.Base64;
import java.util.List;
import java.util.StringTokenizer;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
@Override
public void filter(ContainerRequestContext req) throws
IOException {
//1. read header param
List<String>
authList=headers.getRequestHeader("Authorization");
//if null or empty stop request processing
//return with 400 Response
if(authList==null || authList.isEmpty()) {
req.abortWith(
Response.status(Status.BAD_REQUEST)
.entity("No Auth Data is provided")
.build()
);
return;
}else { //if Header Param exist
String auth=authList.get(0);
//--remove basic space
auth=auth.replaceAll("Basic ", "");
//-- decode data--
byte[]
arr=Base64.getDecoder().decode(auth.getBytes());
//--convert to String--
auth=new String(arr);
//******************************
// Tokenize data
//****************************
StringTokenizer str=new StringTokenizer(auth,":");
Page 3 of 5
by RAGHU SIR[NARESH IT, HYD]
String user=str.nextToken();
String pwd=str.nextToken();
AppConfig
package in.nit.config;
import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;
import in.nit.validator.UserValidator;
@ApplicationPath("/rest")
public class AppConfig
extends ResourceConfig
{
public AppConfig() {
packages("in.nit");
//register filter
register(UserValidator.class);
}
}
PaymentRestController
Page 4 of 5
by RAGHU SIR[NARESH IT, HYD]
package in.nit.controller;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
@Path("/payment")
public class PaymentRestController {
@GET
@Path("/payAmt")
public String doBillPay() {
return "PAYMENT IS DONE";
}
}
pom.xml
<properties>
<failOnMissingWebXml>false</failOnMissingWebXml>
<maven.compiler.source>13</maven.compiler.source>
<maven.compiler.target>13</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.30</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.30</version>
</dependency>
</dependencies>
Page 5 of 5