相關博文html
JavaWeb-SpringBoot_一個類實現騰訊雲SDK發送短信 傳送門java
系列博文mysql
項目已上傳至guthub 傳送門git
JavaWeb-SpringSecurity初認識 傳送門github
JavaWeb-SpringSecurity在數據庫中查詢登錄用戶 傳送門web
JavaWeb-SpringSecurity自定義登錄頁面 傳送門ajax
JavaWeb-SpringSecurity實現需求-判斷請求是否以html結尾 傳送門spring
JavaWeb-SpringSecurity自定義登錄配置 傳送門sql
JavaWeb-SpringSecurity圖片驗證ImageCode 傳送門數據庫
JavaWeb-SpringSecurity記住我功能 傳送門
JavaWeb-SpringSecurity使用短信驗證碼登錄 傳送門
在項目GaryRESTful.validate.code包下建立SmsCode.java
package com.Gary.GaryRESTful.validate.code; import java.time.LocalDateTime; public class SmsCode { //發送短信code private String code; //當前系統時間 private LocalDateTime expireTime; public SmsCode(String code,int exprieTime) { this.code = code; this.expireTime = LocalDateTime.now().plusSeconds(exprieTime); } public SmsCode(String code,LocalDateTime expireTime) { this.code = code; this.expireTime = expireTime; } //判斷當前時間是否存在過時以後 public boolean isExpired() { return LocalDateTime.now().isAfter(expireTime); } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public LocalDateTime getExpireTime() { return expireTime; } public void setExpireTime(LocalDateTime expireTime) { this.expireTime = expireTime; } }
優化ImageCode.java與SmsCode.java,將SmsCode.java重命名爲ValidateCode.java,並用ImageCode.java繼承ValidateCode.java
package com.Gary.GaryRESTful.validate.code; import java.time.LocalDateTime; public class ValidateCode { //發送短信code private String code; //當前系統時間 private LocalDateTime expireTime; public ValidateCode(String code,int expireTime) { this.code = code; this.expireTime = LocalDateTime.now().plusSeconds(expireTime); } public ValidateCode(String code,LocalDateTime expireTime) { this.code = code; this.expireTime = expireTime; } //判斷當前時間是否存在過時以後 public boolean isExpired() { return LocalDateTime.now().isAfter(expireTime); } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public LocalDateTime getExpireTime() { return expireTime; } public void setExpireTime(LocalDateTime expireTime) { this.expireTime = expireTime; } }
導入騰訊雲發短信qcloudsms-1.0.5.jar包
ValidateCodeController.java中實現接收發送短信驗證碼的請求@GetMapping("/code/sms")
@GetMapping("/code/sms") public void createSmsCode(HttpServletRequest request,HttpServletResponse response) throws ServletRequestBindingException { //生成短信的校驗碼 ValidateCode smsCode = createSmsCode(); //將咱們的校驗碼放入session域中 sessionStrategy.setAttribute(new ServletWebRequest(request), sessionSmsKey, smsCode); //從request域中獲取手機號 String mobile = ServletRequestUtils.getRequiredStringParameter(request, "mobile"); //發短信(給mobile手機號發送smsCode驗證碼) sendSms(mobile,smsCode.getCode()); } //發短信(給mobile手機號發送smsCode驗證碼) private void sendSms(String mobile, String code) { //1.騰訊雲本身項目的AppID int appid = 1400182502; String appkey = "58f61b731363faba756087b9504bff46"; int templateId =193928; String smsSign = "Garyd公衆號"; String phoneNumber = mobile; String[] params = new String[1]; params[0] = code; //將驗證碼打印出來 System.out.println("驗證碼: "+code); SmsSingleSender sender = new SmsSingleSender(appid,appkey); //86,手機號,模板id,驗證碼,smsSign try { SmsSingleSenderResult result = sender.sendWithParam("86", phoneNumber, templateId, params, smsSign, "", ""); //打印是否發送成功 System.out.println(result); } catch (JSONException | HTTPException | IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //生成短信的校驗碼 private ValidateCode createSmsCode() { Random r = new Random(); String code = "" +r.nextInt(10)+r.nextInt(10)+r.nextInt(10)+r.nextInt(10); return new ValidateCode(code,120); }
別忘了在SecurityConfig.java中的configure()方法中打開對/code/sms請求的攔截
//在訪問咱們的URL時,咱們是不須要省份認證,能夠當即訪問 .antMatchers("/login.html","/require","/code/image","/code/sms").permitAll()
測試
發送驗證碼請求 localhost:8080/code/sms?mobile=17689470428
後臺result信息輸出
【出現result:1031錯誤表示短信包沒錢了!( ╯□╰ )窘迫~】
當短信發送成功時,result返回值是0,返回值是"OK"
【當控制檯輸出result第一個參數爲 0 後稍等幾秒手機便會接收騰訊雲發送的短信】
#datasource spring.datasource.url=jdbc:mysql:///springsecurity?serverTimezone=UTC&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=123456 spring.datasource.dricer-class-name=com.mysql.jdbc.Driver #jpa #打印出數據庫語句 spring.jpa.show-sql=true #更新數據庫表 spring.jpa.hibernate.ddl-auto=update #配置登錄方式 gary.security.loginType = JSON server.port=8080 #驗證碼長度 gary.security.code.image.length = 6 #驗證碼圖片的長 gary.security.code.image.width = 100 #配置哪些須要咱們驗證碼的Filter gary.security.code.image.url = /user,/user/* #Token過時時間 gary.security.rememberMeSeconds = 3600
package com.Gary.GaryRESTful.config; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl; import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository; import com.Gary.GaryRESTful.filter.ValidateCodeFilter; import com.Gary.GaryRESTful.handler.LoginFailureHandler; import com.Gary.GaryRESTful.handler.LoginSuccessHandler; import com.Gary.GaryRESTful.properties.GarySecurityProperties; //Web應用安全適配器 @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter{ //告訴SpringSecurity密碼用什麼加密的 @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } @Autowired private LoginSuccessHandler loginSuccessHandler; @Autowired private LoginFailureHandler loginFailureHandler; @Autowired private GarySecurityProperties garySecurityProperties; @Autowired private DataSource dataSource; //負責操做數據庫 public PersistentTokenRepository persistentTokenRepository() { JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl(); tokenRepository.setDataSource(dataSource); return tokenRepository; } @Autowired public UserDetailsService userDetailService; protected void configure(HttpSecurity http) throws Exception{ //聲明咱們本身寫的過濾器 ValidateCodeFilter validateCodeFilter = new ValidateCodeFilter(); //給過濾器賦值 validateCodeFilter.setAuthenticationFailureHandler(loginFailureHandler); validateCodeFilter.setGarySecurityProperties(garySecurityProperties); validateCodeFilter.afterPropertiesSet(); //表單驗證(身份認證) http.addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class) .formLogin() //自定義登錄頁面 .loginPage("/require") //若是URL爲loginPage,則用SpringSecurity中自帶的過濾器去處理該請求 .loginProcessingUrl("/loginPage") //配置登錄成功調用loginSuccessHandler .successHandler(loginSuccessHandler) //配置登錄失敗調用loginFailureHandler .failureHandler(loginFailureHandler) //記住我功能 .and() .rememberMe() //配置persistentTokenRepository .tokenRepository(persistentTokenRepository()) //配置過時秒數 .tokenValiditySeconds(garySecurityProperties.getRememberMeSeconds()) //配置userDetailsService .userDetailsService(userDetailService) .and() //請求受權 .authorizeRequests() //在訪問咱們的URL時,咱們是不須要省份認證,能夠當即訪問 .antMatchers("/login.html","/require","/code/image","/code/sms").permitAll() //全部請求都被攔截,跳轉到(/login請求中) .anyRequest() //都須要咱們身份認證 .authenticated() //SpringSecurity保護機制 .and().csrf().disable(); } }
package com.Gary.GaryRESTful.controller; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Random; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.social.connect.web.HttpSessionSessionStrategy; import org.springframework.social.connect.web.SessionStrategy; import org.springframework.web.bind.ServletRequestBindingException; import org.springframework.web.bind.ServletRequestUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.context.request.ServletWebRequest; import com.Gary.GaryRESTful.properties.GarySecurityProperties; import com.Gary.GaryRESTful.validate.code.ImageCode; import com.Gary.GaryRESTful.validate.code.ValidateCode; import com.github.qcloudsms.SmsSingleSender; import com.github.qcloudsms.SmsSingleSenderResult; import com.github.qcloudsms.httpclient.HTTPException; @RestController public class ValidateCodeController { //操做Session private SessionStrategy sessionStrategy = new HttpSessionSessionStrategy(); public static String sessionKey = "session_key_image_code"; public static String sessionSmsKey = "session_key_sms_code"; @Autowired private GarySecurityProperties garySecurityProperties; @GetMapping("/code/sms") public void createSmsCode(HttpServletRequest request,HttpServletResponse response) throws ServletRequestBindingException { //生成短信的校驗碼 ValidateCode smsCode = createSmsCode(); //將咱們的校驗碼放入session域中 sessionStrategy.setAttribute(new ServletWebRequest(request), sessionSmsKey, smsCode); //從request域中獲取手機號 String mobile = ServletRequestUtils.getRequiredStringParameter(request, "mobile"); //發短信(給mobile手機號發送smsCode驗證碼) sendSms(mobile,smsCode.getCode()); } //發短信(給mobile手機號發送smsCode驗證碼) private void sendSms(String mobile, String code) { //1.騰訊雲本身項目的AppID int appid = 1400184301; String appkey = "58f61b731363faba756087b9504bff46"; //短信正文的id int templateId =275243; String smsSign = "Garyd公衆號"; String phoneNumber = mobile; String[] params = new String[1]; params[0] = code; //將驗證碼打印出來 System.out.println("驗證碼: "+code); SmsSingleSender sender = new SmsSingleSender(appid,appkey); //86,手機號,模板id,驗證碼,smsSign try { SmsSingleSenderResult result = sender.sendWithParam("86", phoneNumber, templateId, params, smsSign, "", ""); //打印是否發送成功 System.out.println(result); } catch (JSONException | HTTPException | IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //生成短信的校驗碼 private ValidateCode createSmsCode() { Random r = new Random(); String code = "" +r.nextInt(10)+r.nextInt(10)+r.nextInt(10)+r.nextInt(10); return new ValidateCode(code,120); } @GetMapping("/code/image") public void createCode(HttpServletRequest request,HttpServletResponse response) throws IOException { //生成隨機數的圖片 ImageCode imageCode = createImageCode(request); //將隨機數放入到session中 sessionStrategy.setAttribute(new ServletWebRequest(request), sessionKey, imageCode); //將咱們生成的圖片寫到接口的響應的輸出流中 ImageIO.write(imageCode.getImage(), "JPEG", response.getOutputStream()); } //生成圖片驗證碼(驗證碼,圖片,失效的時間) private ImageCode createImageCode(HttpServletRequest request) { //定義圖片的長和寬 int width = ServletRequestUtils.getIntParameter(request, "width", garySecurityProperties.getCode().getImage().getWidth()); int height = ServletRequestUtils.getIntParameter(request, "height", garySecurityProperties.getCode().getImage().getHeight());; //生成一張圖片 BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); //得到畫筆工具 Graphics g = image.getGraphics(); //畫一個矩形 g.setColor(new Color(255,255,255)); g.fillRect(0, 0, width, height); //畫干擾線 g.setColor(new Color(0,0,0)); //設置字體 g.setFont(new Font("Time New Roman",Font.ITALIC,20)); Random random = new Random(); for(int i=0;i<20;i++) { int x = random.nextInt(width); int y = random.nextInt(height); int x1 = random.nextInt(12); int y1 = random.nextInt(12); //(x,y)到(x+x1,y+y1) g.drawLine(x, y, x+x1, y+y1); } //畫數據 String sRand = ""; for(int i = 0;i<garySecurityProperties.getCode().getImage().getLength();i++) { String rand =String.valueOf(random.nextInt(10)); //System.out.println(rand); sRand += rand; //每個字都改變一下顏色 g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110))); //畫每個數據 g.drawString(rand, 13*i, 16); } g.dispose(); //生成咱們本身的驗證碼數據(圖片,驗證碼,過時時間) return new ImageCode(image,sRand,garySecurityProperties.getCode().getImage().getExpireIn()); } public SessionStrategy getSessionStrategy() { return sessionStrategy; } public void setSessionStrategy(SessionStrategy sessionStrategy) { this.sessionStrategy = sessionStrategy; } public static String getSessionKey() { return sessionKey; } public static void setSessionKey(String sessionKey) { ValidateCodeController.sessionKey = sessionKey; } }
在GaryRESTful.properties包下建立SmsCodeProperties.java,配置Sms發送短信的配置
package com.Gary.GaryRESTful.properties; public class SmsCodeProperties { private int length = 6; private int expireIn = 120; private String url; public int getLength() { return length; } public void setLength(int length) { this.length = length; } public int getExpireIn() { return expireIn; } public void setExpireIn(int expireIn) { this.expireIn = expireIn; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } }
優化ImageCodeProperties.java與SmsCodeProperties.java,將ImageCodeProperties.java繼承SmsCodeProperties.java,並於少寫代碼
package com.Gary.GaryRESTful.properties; public class ImageCodeProperties extends SmsCodeProperties{ private int width = 67; private int height = 23; public ImageCodeProperties() { setLength(4); } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } }
在login.html中添加填寫手機驗證碼<input>,發送驗證碼的手機號咱們給個定值value=17689470428,發送請求時都是經過ajax去進行數據交互,這裏直接給個定製,經過<a>標籤去訪問請求/code/sms?mobile=17689470428(手機號不變)
<form action=""> 手機號: <input type="text" name=「mobile」 value="17689470428"> <br> 短信驗證碼: <input type="text" name="smsCode"> <a href="/code/sms?mobile=17689470428">發送短信驗證碼</a> <input type = "submit"> </form>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>Gary登錄頁面</h1> <form action="/loginPage" method="post"> 用戶名: <input type="text" name="username"> <br> 密碼: <input type="password" name="password"> <br> 圖片驗證碼: <input type="text" name="imageCode"> <img src="/code/image"> <br> <input name="remember-me" type="checkbox" value="true"> 記住我 <input type="submit"> </form> <br> <hr> <br> <form action=""> 手機號: <input type="text" name=「mobile」 value="17689470428"> <br> 短信驗證碼: <input type="text" name="smsCode"> <a href="/code/sms?mobile=17689470428">發送短信驗證碼</a> <input type = "submit"> </form> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>Gary登錄頁面</h1> <form action="/loginPage" method="post"> 用戶名: <input type="text" name="username"> <br> 密碼: <input type="password" name="password"> <br> 圖片驗證碼: <input type="text" name="imageCode"> <img src="/code/image"> <br> <input name="remember-me" type="checkbox" value="true"> 記住我 <input type="submit"> </form> <br> <hr> <br> <form action=""> 手機號: <input type="text" name=「mobile」 value="17689470428"> <br> 短信驗證碼: <input type="text" name="smsCode"> <a href="/code/sms?mobile=17689470428">發送短信驗證碼</a> <input type = "submit"> </form> </body> </html>
#datasource spring.datasource.url=jdbc:mysql:///springsecurity?serverTimezone=UTC&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=123456 spring.datasource.dricer-class-name=com.mysql.jdbc.Driver #jpa #打印出數據庫語句 spring.jpa.show-sql=true #更新數據庫表 spring.jpa.hibernate.ddl-auto=update #配置登錄方式 gary.security.loginType = JSON server.port=8080 #驗證碼長度 gary.security.code.image.length = 6 #驗證碼圖片的長 gary.security.code.image.width = 100 #配置哪些須要咱們驗證碼的Filter gary.security.code.image.url = /user,/user/* #Token過時時間 gary.security.rememberMeSeconds = 3600
package com.Gary.GaryRESTful.config; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl; import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository; import com.Gary.GaryRESTful.filter.ValidateCodeFilter; import com.Gary.GaryRESTful.handler.LoginFailureHandler; import com.Gary.GaryRESTful.handler.LoginSuccessHandler; import com.Gary.GaryRESTful.properties.GarySecurityProperties; //Web應用安全適配器 @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter{ //告訴SpringSecurity密碼用什麼加密的 @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } @Autowired private LoginSuccessHandler loginSuccessHandler; @Autowired private LoginFailureHandler loginFailureHandler; @Autowired private GarySecurityProperties garySecurityProperties; @Autowired private DataSource dataSource; //負責操做數據庫 public PersistentTokenRepository persistentTokenRepository() { JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl(); tokenRepository.setDataSource(dataSource); return tokenRepository; } @Autowired public UserDetailsService userDetailService; protected void configure(HttpSecurity http) throws Exception{ //聲明咱們本身寫的過濾器 ValidateCodeFilter validateCodeFilter = new ValidateCodeFilter(); //給過濾器賦值 validateCodeFilter.setAuthenticationFailureHandler(loginFailureHandler); validateCodeFilter.setGarySecurityProperties(garySecurityProperties); validateCodeFilter.afterPropertiesSet(); //表單驗證(身份認證) http.addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class) .formLogin() //自定義登錄頁面 .loginPage("/require") //若是URL爲loginPage,則用SpringSecurity中自帶的過濾器去處理該請求 .loginProcessingUrl("/loginPage") //配置登錄成功調用loginSuccessHandler .successHandler(loginSuccessHandler) //配置登錄失敗調用loginFailureHandler .failureHandler(loginFailureHandler) //記住我功能 .and() .rememberMe() //配置persistentTokenRepository .tokenRepository(persistentTokenRepository()) //配置過時秒數 .tokenValiditySeconds(garySecurityProperties.getRememberMeSeconds()) //配置userDetailsService .userDetailsService(userDetailService) .and() //請求受權 .authorizeRequests() //在訪問咱們的URL時,咱們是不須要省份認證,能夠當即訪問 .antMatchers("/login.html","/require","/code/image","/code/sms").permitAll() //全部請求都被攔截,跳轉到(/login請求中) .anyRequest() //都須要咱們身份認證 .authenticated() //SpringSecurity保護機制 .and().csrf().disable(); } }
package com.Gary.GaryRESTful.controller; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Random; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.social.connect.web.HttpSessionSessionStrategy; import org.springframework.social.connect.web.SessionStrategy; import org.springframework.web.bind.ServletRequestBindingException; import org.springframework.web.bind.ServletRequestUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.context.request.ServletWebRequest; import com.Gary.GaryRESTful.properties.GarySecurityProperties; import com.Gary.GaryRESTful.validate.code.ImageCode; import com.Gary.GaryRESTful.validate.code.ValidateCode; import com.github.qcloudsms.SmsSingleSender; import com.github.qcloudsms.SmsSingleSenderResult; import com.github.qcloudsms.httpclient.HTTPException; @RestController public class ValidateCodeController { //操做Session private SessionStrategy sessionStrategy = new HttpSessionSessionStrategy(); public static String sessionKey = "session_key_image_code"; public static String sessionSmsKey = "session_key_sms_code"; @Autowired private GarySecurityProperties garySecurityProperties; @GetMapping("/code/sms") public void createSmsCode(HttpServletRequest request,HttpServletResponse response) throws ServletRequestBindingException { //生成短信的校驗碼 ValidateCode smsCode = createSmsCode(); //將咱們的校驗碼放入session域中 sessionStrategy.setAttribute(new ServletWebRequest(request), sessionSmsKey, smsCode); //從request域中獲取手機號 String mobile = ServletRequestUtils.getRequiredStringParameter(request, "mobile"); //發短信(給mobile手機號發送smsCode驗證碼) sendSms(mobile,smsCode.getCode()); } //發短信(給mobile手機號發送smsCode驗證碼) private void sendSms(String mobile, String code) { //1.騰訊雲本身項目的AppID int appid = 1400184301; String appkey = "58f61b731363faba756087b9504bff46"; //短信正文的id int templateId =275243; String smsSign = "Garyd公衆號"; String phoneNumber = mobile; String[] params = new String[1]; params[0] = code; //將驗證碼打印出來 System.out.println("驗證碼: "+code); SmsSingleSender sender = new SmsSingleSender(appid,appkey); //86,手機號,模板id,驗證碼,smsSign try { SmsSingleSenderResult result = sender.sendWithParam("86", phoneNumber, templateId, params, smsSign, "", ""); //打印是否發送成功 System.out.println(result); } catch (JSONException | HTTPException | IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //生成短信的校驗碼 private ValidateCode createSmsCode() { Random r = new Random(); String code = "" ; for(int i=0;i<garySecurityProperties.getCode().getSms().getLength();i++) { code += r.nextInt(10); } return new ValidateCode(code,garySecurityProperties.getCode().getSms().getExpireIn()); } @GetMapping("/code/image") public void createCode(HttpServletRequest request,HttpServletResponse response) throws IOException { //生成隨機數的圖片 ImageCode imageCode = createImageCode(request); //將隨機數放入到session中 sessionStrategy.setAttribute(new ServletWebRequest(request), sessionKey, imageCode); //將咱們生成的圖片寫到接口的響應的輸出流中 ImageIO.write(imageCode.getImage(), "JPEG", response.getOutputStream()); } //生成圖片驗證碼(驗證碼,圖片,失效的時間) private ImageCode createImageCode(HttpServletRequest request) { //定義圖片的長和寬 int width = ServletRequestUtils.getIntParameter(request, "width", garySecurityProperties.getCode().getImage().getWidth()); int height = ServletRequestUtils.getIntParameter(request, "height", garySecurityProperties.getCode().getImage().getHeight());; //生成一張圖片 BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); //得到畫筆工具 Graphics g = image.getGraphics(); //畫一個矩形 g.setColor(new Color(255,255,255)); g.fillRect(0, 0, width, height); //畫干擾線 g.setColor(new Color(0,0,0)); //設置字體 g.setFont(new Font("Time New Roman",Font.ITALIC,20)); Random random = new Random(); for(int i=0;i<20;i++) { int x = random.nextInt(width); int y = random.nextInt(height); int x1 = random.nextInt(12); int y1 = random.nextInt(12); //(x,y)到(x+x1,y+y1) g.drawLine(x, y, x+x1, y+y1); } //畫數據 String sRand = ""; for(int i = 0;i<garySecurityProperties.getCode().getImage().getLength();i++) { String rand =String.valueOf(random.nextInt(10)); //System.out.println(rand); sRand += rand; //每個字都改變一下顏色 g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110))); //畫每個數據 g.drawString(rand, 13*i, 16); } g.dispose(); //生成咱們本身的驗證碼數據(圖片,驗證碼,過時時間) return new ImageCode(image,sRand,garySecurityProperties.getCode().getImage().getExpireIn()); } public SessionStrategy getSessionStrategy() { return sessionStrategy; } public void setSessionStrategy(SessionStrategy sessionStrategy) { this.sessionStrategy = sessionStrategy; } public static String getSessionKey() { return sessionKey; } public static void setSessionKey(String sessionKey) { ValidateCodeController.sessionKey = sessionKey; } }
package com.Gary.GaryRESTful.properties; public class ValidateCodeProperties { //圖片驗證碼 private ImageCodeProperties image = new ImageCodeProperties(); private SmsCodeProperties sms = new SmsCodeProperties(); public SmsCodeProperties getSms() { return sms; } public void setSms(SmsCodeProperties sms) { this.sms = sms; } public ImageCodeProperties getImage() { return image; } public void setImage(ImageCodeProperties image) { this.image = image; } }