本文整理汇总了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;
}