本文整理汇总了Java中javax.servlet.ServletRequest.getRemoteAddr方法的典型用法代码示例。如果您正苦于以下问题:Java ServletRequest.getRemoteAddr方法的具体用法?Java ServletRequest.getRemoteAddr怎么用?Java ServletRequest.getRemoteAddr使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.servlet.ServletRequest
的用法示例。
在下文中一共展示了ServletRequest.getRemoteAddr方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doFilter
import javax.servlet.ServletRequest; //导入方法依赖的package包/类
/**
* Resolves the requester hostname and delegates the request to the chain.
* <p>
* The requester hostname is available via the {@link #get} method.
*
* @param request servlet request.
* @param response servlet response.
* @param chain filter chain.
*
* @throws IOException thrown if an IO error occurrs.
* @throws ServletException thrown if a servet error occurrs.
*/
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
try {
String hostname;
try {
String address = request.getRemoteAddr();
if (address != null) {
hostname = InetAddress.getByName(address).getCanonicalHostName();
} else {
log.warn("Request remote address is NULL");
hostname = "???";
}
} catch (UnknownHostException ex) {
log.warn("Request remote address could not be resolved, {0}", ex.toString(), ex);
hostname = "???";
}
HOSTNAME_TL.set(hostname);
chain.doFilter(request, response);
} finally {
HOSTNAME_TL.remove();
}
}
示例2: doFilter
import javax.servlet.ServletRequest; //导入方法依赖的package包/类
@Override
public void doFilter(ServletRequest reg, ServletResponse res, FilterChain chain) throws IOException,
ServletException {
RequestDispatcher reqDispatcher = reg.getRequestDispatcher("deny.html");
String remoteIP = reg.getRemoteAddr();// 获取访问服务器的IP
String[] ips = null;
boolean isAllow = false;
Pattern pattern = null;
if (!StringUtils.isEmpty(notFilteredIPs)) {
ips = notFilteredIPs.split(";");
for (String ip : ips) {
pattern = Pattern.compile(ip);
if (pattern.matcher(remoteIP).find()) {
isAllow = true;
break;
}
}
} else {
isAllow = true;
}
if (isAllow) {
// 如果不是要过滤的IP就放行,交给下一个Filter
chain.doFilter(reg, res);
} else {
logger.error("访问服务器的IP:" + remoteIP + " , 该IP地址无法访问");
// 如果是要过滤的IP,就通过reqDispatcher跳转到错误页面。
reqDispatcher.forward(reg, res);
}
}
示例3: serviceInvokeRequest
import javax.servlet.ServletRequest; //导入方法依赖的package包/类
private void serviceInvokeRequest(
ServletRequest request,
ServletResponse response,
ObjectInputStream in,
CopyOnReadInputStream copyOnReadInputStream) throws Exception
{
this.methodInvocations++;
//if (CAUSE_RANDOM_ERROR) if (Math.random() > ERROR_RATE) throw new IOException("Random error, for testing only!");
RequestId requestId = (RequestId) in.readObject();
Context invokeContext = ContextCache.getInstance().getOrCreateContext(requestId);
String serviceInterface = (String) in.readObject();
ServiceDefinition serviceDefinition = (ServiceDefinition) this.serviceMap.get(serviceInterface);
if (serviceDefinition == null)
{
invokeContext.setReturnValue(new JrpipRuntimeException("JrpipServlet is not servicing "
+ serviceInterface), true);
}
else
{
OutputStreamBuilder outputStreamBuilder = serviceDefinition.getOutputStreamBuilder();
DataOutputStream copyTo = outputStreamBuilder.newOutputStream();
copyTo.writeByte(OutputStreamBuilder.REQUEST_HEADER);
try
{
if (copyOnReadInputStream != null)
{
copyOnReadInputStream.startCopyingInto(copyTo);
}
invokeContext.setOutputStreamBuilder(outputStreamBuilder);
boolean serviceRequest = true;
if (serviceDefinition.isVmBound())
{
serviceRequest = this.checkVmBoundCall(requestId, invokeContext, serviceInterface, serviceRequest);
}
if (serviceRequest)
{
JrpipRequestContext requestContext = null;
if (request instanceof HttpServletRequest && this.methodInterceptor != null)
{
requestContext = new JrpipRequestContext(
requestId,
((HttpServletRequest) request).getRemoteUser(),
request.getRemoteAddr(),
((HttpServletRequest) request).getCookies());
}
new StreamBasedInvocator().invoke(in,
invokeContext,
serviceDefinition.getService(),
serviceDefinition.getMethodResolver(),
request.getRemoteAddr(),
requestId,
this.listeners,
copyTo,
this.methodInterceptor,
requestContext);
}
}
finally
{
copyTo.close();
}
}
invokeContext.writeAndLogResponse(response.getOutputStream(), requestId);
}
示例4: doFilter
import javax.servlet.ServletRequest; //导入方法依赖的package包/类
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
// Only require authentication from an identity provider if the NiFi registry is running securely.
if (!servletRequest.isSecure()) {
// Otherwise, requests will be "authenticated" by the AnonymousIdentityFilter
filterChain.doFilter(servletRequest, servletResponse);
return;
}
if (identityProvider == null) {
logger.warn("Identity Filter configured with NULL identity provider. Credentials will not be extracted.");
filterChain.doFilter(servletRequest, servletResponse);
return;
}
if (credentialsAlreadyPresent()) {
logger.debug("Credentials already extracted for {}, skipping credentials extraction filter for {}",
SecurityContextHolder.getContext().getAuthentication().getPrincipal(),
identityProvider.getClass().getSimpleName());
filterChain.doFilter(servletRequest, servletResponse);
return;
}
logger.debug("Attempting to extract user credentials using {}", identityProvider.getClass().getSimpleName());
try {
AuthenticationRequest authenticationRequest = identityProvider.extractCredentials((HttpServletRequest)servletRequest);
if (authenticationRequest != null) {
Authentication authentication = new AuthenticationRequestToken(authenticationRequest, identityProvider.getClass(), servletRequest.getRemoteAddr());
logger.debug("Adding credentials claim to SecurityContext to be authenticated. Credentials extracted by {}: {}",
identityProvider.getClass().getSimpleName(),
authenticationRequest);
SecurityContextHolder.getContext().setAuthentication(authentication);
// This filter's job, which is merely to search for and extract an identity claim, is done.
// The actual authentication of the identity claim will be handled by a corresponding IdentityAuthenticationProvider
}
} catch (Exception e) {
logger.debug("Exception occurred while extracting credentials:", e);
}
filterChain.doFilter(servletRequest, servletResponse);
}