security图片验证码

开发生成图形验证码的接口:

package com.imooc.security.core.validate.code;

import java.awt.image.BufferedImage;
import java.time.LocalDateTime;

public class ImageCode {
    
	private BufferedImage image;
	
	private String code;//随机数存到session中
	
	private LocalDateTime expireTime;//有效时间

	public ImageCode (BufferedImage image,String code,
			int expireIn) {
		this.image=image;
		this.code=code;
		this.expireTime=LocalDateTime.now().plusSeconds(expireIn);
	}
	
	public ImageCode (BufferedImage image,String code,
			LocalDateTime expireTime) {
		this.image=image;
		this.code=code;
		this.expireTime=expireTime;
	}
	
	public BufferedImage getImage() {
		return image;
	}

	public void setImage(BufferedImage image) {
		this.image = image;
	}

	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;
	}
	
}
package com.imooc.security.core.validate.code;

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.springframework.social.connect.web.HttpSessionSessionStrategy;
import org.springframework.social.connect.web.SessionStrategy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

@RestController
public class ValidateCodeController {
    
	private static final String SESSION_KEY = "SESSION_KEY_IMAGE_CODE";
	
	private SessionStrategy sessionStrategy = new HttpSessionSessionStrategy();
	
	@GetMapping("/code/image")
	public void createCode(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		
		ImageCode imageCode = createImageCode(request);//code放到sesison 第二步
		sessionStrategy.setAttribute(new ServletWebRequest(request), SESSION_KEY, imageCode);
		//将图片写到响应的接口中 第三步
		ImageIO.write(imageCode.getImage(), "JPEG", response.getOutputStream());
	}

	private ImageCode createImageCode(HttpServletRequest request) {
		//第一步 生成随机验证码 可以去网上搜
		int width=67;//宽和高
		int height=23;
		BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
		
		Graphics g = image.getGraphics();
		
		Random random = new Random();
		
		g.setColor(getRandColor(200,250));
		g.fillRect(0, 0, width, height);
		g.setFont(new Font("Times New Roman",Font.ITALIC,20));
		g.setColor(getRandColor(160,200));
		for(int i=0;i<155;i++) {
			int x = random.nextInt(width);
			int y = random.nextInt(height);
			int xl =random.nextInt(12);
			int yl =random.nextInt(12);
			g.drawLine(x, y, x+xl, y+yl);
		}
		
		String sRand = "";
		for (int i = 0; i < 4; i++) {//数字验证码长度
			String rand = String.valueOf(random.nextInt(10));
			sRand +=rand;
			g.setColor(new Color(20 + random.nextInt(110),
					20 + random.nextInt(110),20 + random.nextInt(110)));
			g.drawString(rand, 13*i+6, 16);
		}
		
		g.dispose();
		
		return new ImageCode(image,sRand,60);//有效期60秒
	}
	
	/**
	 * 生成随机背景条纹
	 */
	private Color getRandColor(int fc,int bc) {
		Random random = new Random();
	    if (fc > 255) {
			fc = 255;
		}	
	    if (bc > 255) {
	    	bc = 255;
		}	
	    int r = fc + random.nextInt(bc - fc);
	    int g = fc + random.nextInt(bc - fc);
	    int b = fc + random.nextInt(bc - fc);
	    return new Color(r,g,b);
	}
}

package com.imooc.security.browser;

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.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import com.imooc.security.browser.authentication.ImoocAuthenticationSuccessHandler;
import com.imooc.security.core.properties.SecurityProperties;

@Configuration
public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter{
   
	@Autowired
	private SecurityProperties securityProperties;
	
	//让系统使用我们自定义 而不是系统默认的配置
	@Autowired
	private AuthenticationSuccessHandler imoocAuthenticationSuccessHandler;
	
    @Autowired
    private AuthenticationFailureHandler imoocAuthenticationFailureHandler;
	
	@Bean
	public PasswordEncoder passwordEncoder() {
		//这里如果是自己编写的加密 则调用自己的类 方法有编码和解码验证方法
		return new BCryptPasswordEncoder();
	}
	
	@Override
	protected void configure(HttpSecurity http) throws Exception {
		
		http.formLogin()//认证
		.loginPage("/authentication/require")//设置登录页面
		.loginProcessingUrl("/authentication/form")//遇到该请求则进行user password认证
		.successHandler(imoocAuthenticationSuccessHandler)//成功后 使用我们自己的处理器处理
		.failureHandler(imoocAuthenticationFailureHandler)//设置失败处理器
//		http.httpBasic()
		.and()
		.authorizeRequests()//授权
		//当访问这个路径的时候不需要身份认证 除了它其他的是需要身份认证
		.antMatchers("/authentication/require"
				,securityProperties.getBrowsers().getLoginPage()
				,"/code/image").permitAll()
		.anyRequest()
		.authenticated()
		.and()
		.csrf().disable();
	}
    
}

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="brilliance.znjt.business.workassess.dao.WorkAssessMapper">
      <update id="gwsdsettingDay">
	  		UPDATE T_GWSD_SETTING T 
	  		   SET T.KSSJ = #{kssj},
	  		       T.JSSJ = #{jssj} 
	  		 WHERE T.NAME = '白班'
	  </update>
	  <update id="gwsdsettingNight">
	  		UPDATE T_GWSD_SETTING T 
	  		   SET T.KSSJ = #{kssj},
	  		       T.JSSJ = #{jssj} 
	  		 WHERE T.NAME = '夜班'
	  </update>
	  <insert id="insertWfcf" parameterType="brilliance.znjt.business.workassess.entity.Wfcf">
       INSERT INTO T_WFXW_SCORE
		<trim prefix="(" suffix=")" suffixOverrides=",">
	        JDBH,
	      <if test="jdmc != null and jdmc != ''">
	        JDMC,
	      </if>
	      <if test="wfdm != null and wfdm != ''">
	        WFDM,
	      </if>
	      <if test="ysjd != 0">
	        YSJD,
	      </if>
	      <if test="jdqz != 0">
	        JDQZ,
	      </if>
	      <if test="sjjd != 0">
	        SJJD,
	      </if>
	      <!-- <if test="jdfl != null and jdfl != ''">
	        JDFL,
	      </if> -->
	      <if test="jdsm != null and jdsm != ''">
	        JDSM,
	      </if>
	      <if test="sjly != null and sjly != ''">
	        SJLY,
	      </if>
	      <if test="lrr != null and lrr !=''">
	        LRR,
	      </if>
	        LRSJ,
	      <if test="gxr != null and gxr !=''">
	        GXR,
	      </if>
	    </trim>
	    <trim prefix="values (" suffix=")" suffixOverrides=",">
	      ZNJTPOSTMAN.FPOSTMAN_NEXTVAL('SEQ_T_WFXW_SCORE_CODE'),
	      <if test="jdmc != null and jdmc != ''">
	        #{jdmc,jdbcType=VARCHAR},
	      </if>
	      <if test="wfdm != null and wfdm != ''">
	        #{wfdm,jdbcType=VARCHAR},
	      </if>
	      <if test="ysjd != 0">
	        #{ysjd,jdbcType=INTEGER},
	      </if>
	      <if test="jdqz != 0">
	        #{jdqz,jdbcType=INTEGER},
	      </if>
	      <if test="sjjd != 0">
	        #{sjjd,jdbcType=INTEGER},
	      </if>
	     <!-- <if test="jdfl != null and jdfl != ''">
	        #{jdfl,jdbcType=VARCHAR},
	      </if> -->
	      <if test="jdsm != null and jdsm != ''">
	        #{jdsm,jdbcType=VARCHAR},
	      </if>
	      <if test="sjly != null and sjly != ''">
	        #{sjly,jdbcType=VARCHAR},
	      </if>
	      <if test="lrr != null and lrr !=''">
	        #{lrr,jdbcType=VARCHAR},
	      </if>
	        SYSDATE,
	      <if test="gxr != null and gxr !=''">
	        #{gxr,jdbcType=VARCHAR},
	      </if>
	    </trim>
    </insert>
    <delete id="removeByWfflAndWflx" parameterType="java.lang.String">
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值