⑴ springMVC怎麼配置出https用作登錄
/*編碼轉換,確保寫資料庫的時候不會出現亂碼*/publicclassCodingConvert{publicCodingConvert(){//}publicStringtoGb(StringuniStr){StringgbStr="";if(uniStr==null){uniStr="";}try{byte[]tempByte=uniStr
⑵ springMVC怎麼配置出https用作登錄
首先,要生成SSL證書。 二,配置tomcat,指定證書位置。 三,配置Spring,指定https訪問路徑 可以看看
⑶ springMVC怎麼配置出https用作登錄
首先,要生成SSL證書。
二,配置tomcat,指定證書位置。
三,配置Spring,指定https訪問路徑。
⑷ springMVC怎麼配置出https用作登錄
首先,要生成SSL證書。 二,配置tomcat,指定證書位置。 三,配置Spring,指定https訪問路徑。
⑸ Java實現攔截HTTP請求的幾種方式
在Java的服務端開發當中,攔截器是很常見的業務場景,這里對Java開發當中幾種常見的攔截器的實現方式進行記錄和分析。案例說明基於Spring Boot環境。
一:實現javax.servlet.Filter介面(使用過濾器方式攔截請求)
import org.springframework.stereotype.Component;import javax.servlet.*;import java.io.IOException;import java.util.Date;@Componentpublic class TimeInterceptor implements Filter {@Overridepublic void init(FilterConfig filterConfig) throws ServletException {System.out.println("time filter init");}@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {System.out.println("time filter start");long start = new Date().getTime();filterChain.doFilter(servletRequest, servletResponse);System.out.println("time filter 耗時:"+(new Date().getTime()-start));System.out.println("time filter finish");}@Overridepublic void destroy() {System.out.println("time filter destroy");}}
如使用@Compent註解聲明不需要加入其它配置即可使得攔截器生效,但是默認攔截/*,會攔截所有請求。
二:使用@Bean注入自定義攔截器,依然上面的代碼,去掉@Compent註解,創建TimeWebConfig配置類:
import org.springframework.boot.web.servlet.FilterRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import java.util.ArrayList;import java.util.List;@Configurationpublic class TimeWebConfig {@Beanpublic FilterRegistrationBean timeFilter(){FilterRegistrationBean registrationBean = new FilterRegistrationBean();TimeInterceptor interceptor = new TimeInterceptor();registrationBean.setFilter(interceptor);List<String> urls = new ArrayList<>();urls.add("/user/*");registrationBean.setUrlPatterns(urls);return registrationBean;}}
上面這兩種攔截請求的實現是基於JavaEE提供的Filter介面實現的,缺點在於,該攔截器實際上是一個過濾器,執行代碼的方法doFilter只提供了request,response等參數,當請求進入被過濾器攔截的時候,我們並不知道這個請求是由哪個控制器的哪個方法來執行的。
三:使用springMVC提供的攔截器,實現org.springframework.web.servlet.HandlerInterceptor介面:
創建自定義的攔截器:
import org.springframework.stereotype.Component;import org.springframework.web.method.HandlerMethod;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.util.Date;@Componentpublic class MyInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) throws Exception {System.out.println("preHandler");System.out.println(((HandlerMethod) handler).getBean().getClass().getName());System.out.println(((HandlerMethod) handler).getMethod().getName());httpServletRequest.setAttribute("start", new Date().getTime());return true;}@Overridepublic void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {System.out.println("postHandler");Long start = (Long) httpServletRequest.getAttribute("start");System.out.println("time interceptor 耗時:"+(new Date().getTime()-start));}@Overridepublic void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {System.out.println("afterCompletion");Long start = (Long) httpServletRequest.getAttribute("start");System.out.println("time interceptor 耗時:"+(new Date().getTime()-start));System.out.println("ex is:"+e);}}
創建配置類:
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;@Configurationpublic class WebConfig extends WebMvcConfigurerAdapter {@Autowiredprivate MyInterceptor interceptor;@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(interceptor).addPathPatterns("/user/*").excludePathPatterns("/blog/*");}}
此種方式的攔截器當中我們能夠獲取攔截的請求對應的類和方法的相關信息,缺點在於該handler對象無法獲取具體執行方法的參數信息。
四:利用Spring的切面(AOP)實現攔截器:
引入jar包:
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-aop --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency>
創建切片類:
import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.springframework.stereotype.Component;import java.util.Date;@Aspect@Componentpublic class TimeAspect {@Around("execution(* com.qinker.controller.UserController.*(..))")public Object handlerControllerMethod(ProceedingJoinPoint point) throws Throwable {System.out.println("time aspect start");long start = new Date().getTime();Object[] args = point.getArgs();for (Object obj : args) {System.out.println("arg is:"+obj);}Object obj = point.proceed();//具體方法的返回值System.out.println("aspect 耗時:"+(new Date().getTime()-start));System.out.println("time aspect end");return obj;}}
aspectj基於AOP實現的攔截器功能十分強大,具體詳解請參考spring官網網站的文檔。
⑹ spring boot 怎麼配置的https是永久的嗎
Gworg裡面先申請SSL證書,最長只能1年,沒有永久的。
具體流程:
進入Gworg拿到SSL證書,裡面有JKS證書與密碼。
spring boot根據該技術文檔配置SSL證書:網頁鏈接
伺服器防火牆允許443埠或者其它自定義的HTTPS埠,雲伺服器需要額外在控制面板設置。
解決辦法:進入伺服器選擇JKS證書配置文檔就可以了。
⑺ Java SpringMVC如何開放一個https介面
通過在tomcat的server.xml中可以配置,普通請求在8080埠上,https在8448埠上,具體的配置你可以網上找找看很多。
這樣tomcat就可以完成到調用者的數據傳輸加密。業務層無干擾。
⑻ spring boot怎麼支持https
這個跟springmvc一樣的啊,首先你看你的spring-mvc.xml有沒有配置defaultViewResolver,然後你在action的方法中如果1.標注了@ResponseBody,返回字元串的話是通過write輸出到頁面。2.沒有標注這個,springmvc會到配置的目錄下找相應的jsp。比如返回"hello",它就在webpage/目錄下找hello.jsp。返回"user/login",它就會找webpage/user/login.jsp