本文整理匯總了Java中com.sun.jersey.spi.container.ContainerRequest.setSecurityContext方法的典型用法代碼示例。如果您正苦於以下問題:Java ContainerRequest.setSecurityContext方法的具體用法?Java ContainerRequest.setSecurityContext怎麽用?Java ContainerRequest.setSecurityContext使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.sun.jersey.spi.container.ContainerRequest
的用法示例。
在下文中一共展示了ContainerRequest.setSecurityContext方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: filter
import com.sun.jersey.spi.container.ContainerRequest; //導入方法依賴的package包/類
@Override
public ContainerRequest filter(ContainerRequest request) {
// Get session id from request header
final String sessionId = request.getHeaderValue("session-id");
SessionUser user = null;
Session session = null;
if (sessionId != null && sessionId.length() > 0) {
// Load session object from repository
// session = sessionRepository.findOne(sessionId);
// Load associated user from session
// if (session != null) {
// // user = userRepository.findOne(session.getUserId());
// }
}
request.setSecurityContext(new ElabSecurityContext(session, user));
return request;
}
示例2: filter
import com.sun.jersey.spi.container.ContainerRequest; //導入方法依賴的package包/類
@Override
public ContainerRequest filter(ContainerRequest request) {
// No authentication needed for GET requests.
if(request.getMethod().equalsIgnoreCase("GET")) {
return request;
}
final User user = authenticate(request);
SecurityContext ctxt = new SecurityContext() {
@Override
public boolean isUserInRole(String arg0) {
return true;
}
@Override
public boolean isSecure() {
return false;
}
@Override
public Principal getUserPrincipal() {
return new Principal() {
@Override
public String getName() {
return user.getUsername();
}
};
}
@Override
public String getAuthenticationScheme() {
return SecurityContext.BASIC_AUTH;
}
};
request.setSecurityContext(ctxt);
return request;
}
示例3: filter
import com.sun.jersey.spi.container.ContainerRequest; //導入方法依賴的package包/類
@Override
public ContainerRequest filter(ContainerRequest request) {
// OPTIONS calls don't need authorization, apparently
if ("OPTIONS".equals(request.getMethod())) {
return request;
}
// Authentication via header or cookie
String authentication = request.getHeaderValue(HEADER);
if (StringUtils.isBlank(authentication)) {
Cookie cookie = request.getCookies().get(HEADER);
if (cookie != null) {
authentication = cookie.getValue();
}
}
// Log.info("authentication={}", authentication);
if (StringUtils.isNotBlank(authentication)) {
List<String> parts = Lists.newArrayList(Splitter.on(" ").split(authentication));
if (parts.size() == 2) {
String scheme = parts.get(0);
String key = parts.get(1);
try {
SecurityContext securityContext = sessionService.getSecurityContext(scheme, key);
if (securityContext != null) {
request.setSecurityContext(securityContext);
return request;
}
} catch (nl.knaw.huygens.security.client.UnauthorizedException e) {
throw new UnauthorizedException(e.getMessage());
}
}
} else {
}
throw new UnauthorizedException("No valid " + HEADER + " header in request");
}
示例4: filter
import com.sun.jersey.spi.container.ContainerRequest; //導入方法依賴的package包/類
@Override
public ContainerRequest filter(ContainerRequest request) {
String username = request.getHeaderValue(AuthConstants.USERNAME_HEADER);
String token = request.getHeaderValue(AuthConstants.TOKEN_HEADER);
String auth = request.getHeaderValue(HttpHeaders.AUTHORIZATION);
if (username == null) {
if (auth != null) {
if (!auth.startsWith("Basic ")) {
throw new WebApplicationException(Status.BAD_REQUEST);
}
String decoded = new String(Base64.decode(auth
.replaceFirst("Basic ", "")));
int sep = decoded.indexOf(':');
if (sep >= 0) {
username = decoded.substring(0, sep);
token = decoded.substring(sep + 1);
} else {
throw new WebApplicationException(Status.BAD_REQUEST);
}
}
}
if (username != null) {
if (username.isEmpty() || token == null || token.isEmpty()) {
throw new WebApplicationException(Status.BAD_REQUEST);
}
try {
User user = service.getUser(username);
if (passwordEncoder.verify(token, user.getToken())) {
request.setSecurityContext(
new SecurityContextImpl(user, request.isSecure()));
} else {
throw new WebApplicationException(Status.FORBIDDEN);
}
} catch (UserNotFoundException ex) {
throw new WebApplicationException(ex, Status.FORBIDDEN);
}
}
return request;
}
示例5: filter
import com.sun.jersey.spi.container.ContainerRequest; //導入方法依賴的package包/類
@Override
public ContainerRequest filter(ContainerRequest request) {
User user = authenticate(request);
request.setSecurityContext(new HttpBasicAuthContext(user));
return request;
}