「java获取请求」java获取请求头中的token
本篇文章给大家谈谈java获取请求,以及java获取请求头中的token对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java怎么获取客户端的请求信息
- 2、java如何获取security过滤的请求
- 3、java怎么获取请求的ip
- 4、java中请求参数action怎么获取
- 5、java获取请求域名
- 6、java filter 如何中获取请求地址?
java怎么获取客户端的请求信息
java获取客服端信息(系统,浏览器等)
String agent = request.getHeader("user-agent");
System.out.println(agent);
StringTokenizer st = new StringTokenizer(agent,";");
st.nextToken();
String userbrowser = st.nextToken();
System.out.println(userbrowser);
String useros = st.nextToken();
System.out.println(useros);
System.out.println(System.getProperty("os.name")); //win2003竟然是win xp?
System.out.println(System.getProperty("os.version"));
System.out.println(System.getProperty("os.arch"));
System.out.println(request.getHeader("user-agent")); //返回客户端浏览器的版本号、类型
System.out.println(request.getMethod()); //:获得客户端向服务器端传送数据的方法有get、post、put等类型
System.out.println(request.getRequestURI()); //:获得发出请求字符串的客户端地址
System.out.println(request.getServletPath()); //:获得客户端所请求的脚本文件的文件路径
System.out.println(request.getServerName()); //:获得服务器的名字
System.out.println(request.getServerPort()); //:获得服务器的端口号
System.out.println(request.getRemoteAddr()); //:获得客户端的ip地址
System.out.println(request.getRemoteHost()); //:获得客户端电脑的名字,若失败,则返回客户端电脑的ip地址
System.out.println(request.getProtocol()); //:
System.out.println(request.getHeaderNames()); //:返回所有request header的名字,结果集是一个enumeration(枚举)类的实例
System.out.println("Protocol: " + request.getProtocol());
System.out.println("Scheme: " + request.getScheme());
System.out.println("Server Name: " + request.getServerName() );
System.out.println("Server Port: " + request.getServerPort());
System.out.println("Protocol: " + request.getProtocol());
System.out.println("Server Info: " + getServletConfig().getServletContext().getServerInfo());
System.out.println("Remote Addr: " + request.getRemoteAddr());
System.out.println("Remote Host: " + request.getRemoteHost());
System.out.println("Character Encoding: " + request.getCharacterEncoding());
System.out.println("Content Length: " + request.getContentLength());
System.out.println("Content Type: "+ request.getContentType());
System.out.println("Auth Type: " + request.getAuthType());
System.out.println("HTTP Method: " + request.getMethod());
System.out.println("Path Info: " + request.getPathInfo());
System.out.println("Path Trans: " + request.getPathTranslated());
System.out.println("Query String: " + request.getQueryString());
System.out.println("Remote User: " + request.getRemoteUser());
System.out.println("Session Id: " + request.getRequestedSessionId());
System.out.println("Request URI: " + request.getRequestURI());
System.out.println("Servlet Path: " + request.getServletPath());
System.out.println("Accept: " + request.getHeader("Accept"));
System.out.println("Host: " + request.getHeader("Host"));
System.out.println("Referer : " + request.getHeader("Referer"));
System.out.println("Accept-Language : " + request.getHeader("Accept-Language"));
System.out.println("Accept-Encoding : " + request.getHeader("Accept-Encoding"));
System.out.println("User-Agent : " + request.getHeader("User-Agent"));
System.out.println("Connection : " + request.getHeader("Connection"));
System.out.println("Cookie : " + request.getHeader("Cookie"));
获得user-agent的值
在 ASP.NET 中使用 Request.Header["User-Agent"] 得到浏览器的 User Agent,也可以使用 Request.UserAgent 来获取;
Java 中使用 request.getHeader(”User-Agent”) 来获得;
PHP 中相应使用:$_SERVER[HTTP_USER_AGENT];
JS中则使用navigator.userAgent来获得(客户端经常使用它来做浏览器兼容)。
java如何获取security过滤的请求
Copyright © 1999-2020, CSDN.NET, All Rights Reserved

打开APP

Spring Security过滤器链如何匹配到特定的请求 转载
2022-02-16 14:30:42

Java面试那些事儿 
码龄2年
关注
如何拦截特定的请求
只有满足了SecurityFilterChain的match方法的请求才能被该SecurityFilterChain处理,那如何配置才能让一个SecurityFilterChain处理特定的路径呢?
RequestMatcher
HttpSecurity内置了RequestMatcher属性来处理路径匹配问题。RequestMatcher可总结为以下几大类:

使用Ant路径:
httpSecurity.antMatcher("/foo/**");
如果你配置了全局的Servlet Path的话,例如/v1,配置ant路径的话就要/v1/foo/**,使用MVC风格可以保持一致:
httpSecurity.mvcMatcher("/foo/**");
另外MVC风格可以自动匹配后缀,例如/foo/hello可以匹配/foo/hello.do、/foo/hello.action 等等。另外你也可以使用正则表达式来进行路径匹配:
httpSecurity.regexMatcher("/foo/.+");
如果上面的都满足不了需要的话,你可以通过HttpSecurity.requestMatcher方法自定义匹配规则;如果你想匹配多个规则的话可以借助于HttpSecurity.requestMatchers方法来自由组合匹配规则,就像这样:
httpSecurity.requestMatchers(requestMatchers -
requestMatchers.mvcMatchers("/foo/**")
.antMatchers("/admin/*get"));
一旦你配置了路径匹配规则的话,你会发现默认的表单登录404了,因为默认是/login,你加了前缀后当然访问不到了。
java怎么获取请求的ip
java获取外网ip地址方法:
public class Main {
public static void main(String[] args) throws SocketException {
System.out.println(Main.getRealIp());
}
public static String getRealIp() throws SocketException {
String localip = null;// 本地IP,如果没有配置外网IP则返回它
String netip = null;// 外网IP
EnumerationNetworkInterface netInterfaces =
NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
boolean finded = false;// 是否找到外网IP
while (netInterfaces.hasMoreElements() !finded) {
NetworkInterface ni = netInterfaces.nextElement();
EnumerationInetAddress address = ni.getInetAddresses();
while (address.hasMoreElements()) {
ip = address.nextElement();
if (!ip.isSiteLocalAddress()
!ip.isLoopbackAddress()
ip.getHostAddress().indexOf(":") == -1) {// 外网IP
netip = ip.getHostAddress();
finded = true;
break;
} else if (ip.isSiteLocalAddress()
!ip.isLoopbackAddress()
ip.getHostAddress().indexOf(":") == -1) {// 内网IP
localip = ip.getHostAddress();
}
}
}
if (netip != null !"".equals(netip)) {
return netip;
} else {
return localip;
}
}
}
java中请求参数action怎么获取
1. ActionContext
在Struts2开发中,除了将请求参数自动设置到Action的字段中,我们往往也需要在Action里直接获取请求(Request)或会话(Session)的一些信息,甚至需要直接对JavaServlet Http的请求(HttpServletRequest),响应(HttpServletResponse)操作. 我们需要在Action中取得request请求参数"username"的值:
ActionContext context = ActionContext.getContext();
Map params = context.getParameters();
String username = (String) params.get("username");
on执行时的上下文,上下文可以看作是一个容器(其实我们这里的容器就是一个Map而已),它存放的是Action在执行时需要用到的对象. 一般情况, 我们的ActionContext都是通过: ActionContext context = (ActionContext) actionContext.get();来获取的.我们再来看看这里的actionContext对象的创建:
static ThreadLocal actionContext = new ActionContextThreadLocal();
ActionContextThreadLocal是实现ThreadLocal的一个内部类.ThreadLocal可以命名为"线程局部变量",它为每一个使用该变量的线程都提供一个变量值的副本,使每一个线程都可以独立地改变自己的副本,而不会和其它线程的副本冲突.这样,我们ActionContext里的属性只会在对应的当前请求线程中可见,从而保证它是线程安全的.
通过ActionContext取得HttpSession: Map session = ActionContext.getContext().getSession();
2. ServletActionContext
ServletActionContext(com.opensymphony.webwork. ServletActionContext),这个类直接继承了我们上面介绍的ActionContext,它提供了直接与Servlet相关对象访问的功能,它可以取得的对象有:
(1)javax.servlet.http.HttpServletRequest : HTTPservlet请求对象
(2)javax.servlet.http.HttpServletResponse : HTTPservlet相应对象
(3)javax.servlet.ServletContext : Servlet上下文信息
(4)javax.servlet.ServletConfig : Servlet配置对象
(5)javax.servlet.jsp.PageContext : Http页面上下文
如何从ServletActionContext里取得Servlet的相关对象:
1取得HttpServletRequest对象: HttpServletRequest request = ServletActionContext. getRequest();
2取得HttpSession对象: HttpSession session = ServletActionContext. getRequest().getSession();
3. ServletActionContext和ActionContext联系
ServletActionContext和ActionContext有着一些重复的功能,在我们的Action中,该如何去抉择呢?我们遵循的原则是:如果ActionContext能够实现我们的功能,那最好就不要使用ServletActionContext,让我们的Action尽量不要直接去访问Servlet的相关对象.
注意:在使用ActionContext时有一点要注意: 不要在Action的构造函数里使用ActionContext.getContext(),因为这个时候ActionContext里的一些值也许没有设置,这时通过ActionContext取得的值也许是null;同样,HttpServletRequest req = ServletActionContext.getRequest()也不要放在构造函数中,也不要直接将req作为类变量给其赋值。至于原因,我想是因为前面讲到的static ThreadLocal actionContext = new ActionContextThreadLocal(),从这里我们可以看出ActionContext是线程安全的,而ServletActionContext继承自ActionContext,所以ServletActionContext也线程安全,线程安全要求每个线程都独立进行,所以req的创建也要求独立进行,所以ServletActionContext.getRequest()这句话不要放在构造函数中,也不要直接放在类中,而应该放在每个具体的方法体中(eg:login()、queryAll()、insert()等),这样才能保证每次产生对象时独立的建立了一个req。
4. struts2中获得request、response和session
(1)非IoC方式
方法一:使用org.apache.struts2.ActionContext类,通过它的静态方法getContext()获取当前Action的上下文对象。
ActionContext ctx = ActionContext.getContext();
ctx.put("liuwei", "andy"); //request.setAttribute("liuwei", "andy");
Map session = ctx.getSession(); //session
HttpServletRequest request = ctx.get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);
HttpServletResponse response = ctx.get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE);
细心的朋友可以发现这里的session是个Map对象, 在Struts2中底层的session都被封装成了Map类型. 我们可以直接操作这个Map对象进行对session的写入和读取操作, 而不用去直接操作HttpSession对象.
方法二:使用org.apache.struts2.ServletActionContext类
public class UserAction extends ActionSupport {
//其他代码片段
private HttpServletRequest req;
// private HttpServletRequest req = ServletActionContext.getRequest(); 这条语句放在这个位置是错误的,同样把这条语句放在构造方法中也是错误的。
public String login() {
req = ServletActionContext.getRequest(); //req的获得必须在具体的方法中实现
user = new User();
user.setUid(uid);
user.setPassword(password);
if (userDAO.isLogin(user)) {
req.getSession().setAttribute("user", user);
return SUCCESS;
}
return LOGIN;
}
public String queryAll() {
req = ServletActionContext.getRequest(); //req的获得必须在具体的方法中实现
uList = userDAO.queryAll();
req.getSession().setAttribute("uList", uList);
return SUCCESS;
}
//其他代码片段
}
(2)IoC方式(即使用Struts2 Aware拦截器)
要使用IoC方式,我们首先要告诉IoC容器(Container)想取得某个对象的意愿,通过实现相应的接口做到这点。
public class UserAction extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware {
private HttpServletRequest request;
private HttpServletResponse response;
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
public String execute() {
HttpSession session = request.getSession();
return SUCCESS;
}
}
java获取请求域名
很多朋友都想知道java如何获取请求域名?下面就一起来了解一下吧~
1、获取协议名和域名。
request.getScheme(); //得到协议名 例如:http request.getServerName(); //得到域名 localhost
2、获取全路径。
request.getRequestURL(); //得到
3、获取请求所有参数 //map类型。
request.getParameterMap()
4、获取项目名
request.getContextPath(); // /CRM
5、获取请求方法
request.getServletPath(); // /loginController/login
/** * 获取当前访问URL (含协议、域名、端口号[忽略80端口]、项目名) * @param request * @return: String */ public static String getServerUrl(HttpServletRequest request) { // 访问协议 String agreement = request.getScheme(); // 访问域名 String serverName = request.getServerName(); // 访问端口号 int port = request.getServerPort(); // 访问项目名 String contextPath = request.getContextPath(); String url = "%s://%s%s%s"; String portStr = ""; if (port != 80) { portStr += ":" + port; } return String.format(url, agreement, serverName, portStr, contextPath); }
java filter 如何中获取请求地址?
当我们访问index.jsp/时,其实并不是我们浏览器真正访问到了服务器上的index.jsp 文件,而是先由代理服务器去访问index.jsp。
代理服务器再将访问到的结果返回给我们的浏览器,因为是代理服务器去访问index.jsp的。
所以index.jsp中通过 request.getRemoteAddr()的方法获取的IP实际上是代理服务器的地址,并不是客户端的IP地址。
于是可得出获得客户端真实IP地址 的方法:
public class Myfilter implements Filter {
public void destroy() {
}
private HttpServletRequest request;
private HttpServletResponse response;
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
request = (HttpServletRequest) arg0;
response = (HttpServletResponse) arg1;
System.out.println(request.getRequestURI());
System.out.println("===ff===="+request.getQueryString());
if("1".equals(request.getRequestURI().substring(("/"+request.getContextPath()).length()))){
request.getRequestDispatcher("/my.jsp").forward(request, response);
return;
}
arg2.doFilter(request, arg1);
}
public void init(FilterConfig arg0) throws ServletException {
}
}
java获取请求的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java获取请求头中的token、java获取请求的信息别忘了在本站进行查找喔。