分布式环境中的登录应用Word格式.docx
- 文档编号:7510430
- 上传时间:2023-05-08
- 格式:DOCX
- 页数:39
- 大小:23.28KB
分布式环境中的登录应用Word格式.docx
《分布式环境中的登录应用Word格式.docx》由会员分享,可在线阅读,更多相关《分布式环境中的登录应用Word格式.docx(39页珍藏版)》请在冰点文库上搜索。
view-name="
redirect:
/login"
/>
--对静态资源文件的访问restful-->
resourcesmapping="
/static/**"
location="
/static/"
cache-period="
31556926"
resourceslocation="
/favicon.ico"
mapping="
/webjars/**"
classpath:
/META-INF/resources/webjars/"
/swagger-ui.html"
/META-INF/resources/"
--访问拦截-->
interceptors>
interceptor>
mappingpath="
/**"
exclude-mappingpath="
/*/api-docs"
/configuration/**"
beanclass="
mon.web.interceptor.EventInterceptor"
>
propertyname="
nextInterceptor"
array>
--登录拦截器-->
com.buoumall.front.interceptor.LoginInterceptor"
loginUrl"
value="
/bean>
/array>
/property>
/mvc:
--验证码-->
beanid="
captchaProducer"
class="
com.google.code.kaptcha.impl.DefaultKaptcha"
config"
com.google.code.kaptcha.util.Config"
constructor-arg>
props>
propkey="
kaptcha.border"
no<
/prop>
kaptcha.border.color"
105,179,90<
kaptcha.textproducer.font.color"
red<
kaptcha.image.width"
250<
kaptcha.textproducer.font.size"
90<
kaptcha.image.height"
120<
kaptcha.session.key"
code<
kaptcha.textproducer.char.length"
4<
kaptcha.textproducer.font.names"
宋体,楷体,微软雅黑<
/props>
/constructor-arg>
3.生成验证码的controller
[java]viewplaincopy在CODE上查看代码片派生到我的代码片
//生成验证码
@RequestMapping("
/captcha-image"
)
publicvoidcaptchaImageCode(@RequestParamIntegerbizType,HttpServletRequestrequest,HttpServletResponseresponse)throwsBizException{
response.setDateHeader("
Expires"
0);
//SetstandardHTTP/1.1no-cacheheaders.
response.setHeader("
Cache-Control"
"
no-store,no-cache,must-revalidate"
);
//SetIEextendedHTTP/1.1no-cacheheaders(useaddHeader).
response.addHeader("
"
post-check=0,pre-check=0"
//SetstandardHTTP/1.0no-cacheheader.
Pragma"
no-cache"
//returnajpeg
response.setContentType("
image/png"
//createthetextfortheimage
StringcapText=captchaProducer.createText();
//bizType为业务类型,这里为LOGIN
BizTypeEnumbizTypeEnum=BizTypeEnum.findByBizType(bizType);
if(bizTypeEnum==null){
logger.error("
ThebizTypeisillegal!
"
return;
}
//客户端保存一个验证码的cookie,GlobalConst.BOM_CAPTCHA_KEY为cookie的name,value为一个随机数
Stringcaptcha_key=CookieUtil.getCookieValue(request,GlobalConst.BOM_CAPTCHA_KEY);
//如果取出来的为空,这表示是一个新用户,客户端没有这个cookie,就新建一个
if(StringUtils.isBlank(captcha_key)){
captcha_key=UuidUtil.get32UUID();
logger.info("
Newusercoming,captcha_key={}"
captcha_key);
CookieUtil.addCookie(response,GlobalConst.BOM_CAPTCHA_KEY,captcha_key,CaptchaCodeUtil.CAPTCHA_TIME_OUT);
//将验证码和客户端cookie的信息存在redis中
CaptchaCodeUtil.addCaptchaCode(captcha_key,redisService,bizTypeEnum,capText);
OutputStreamout=null;
try{
//createtheimagewiththetext
BufferedImagebi=captchaProducer.createImage(capText);
out=response.getOutputStream();
//writethedataout
ImageIO.write(bi,"
png"
out);
}catch(IOExceptione){
e.printStackTrace();
}finally{
IOUtils.closeQuietly(out);
}
//判断验证码是否正确
//一般是先判断验证码是否正确,如果正确再进行登录判断,不然提示验证码错误。
@RequestMapping(value="
/checkCaptchaCode"
@ResponseBody
publicResult<
Boolean>
checkCaptchaCode(HttpServletRequestrequest,
@RequestParamIntegerbizType,@RequestParamStringcaptchaCode){
//bizType表示业务类型,这里是LOGIN
returnsuper.buildBizErrorResult("
验证码bizType不合法"
//得到客户端cookie的value,redis里的key就是通过这个value生成的
StringcaptchaKey=CookieUtil.getCookieValue(request,GlobalConst.BOM_CAPTCHA_KEY);
//在redis中判断验证码是否正确
booleancaptchaCodeOk=CaptchaCodeUtil.captchaCodeOk(captchaKey,redisService,bizTypeEnum,captchaCode);
if(captchaCodeOk){
returnsuper.buildOk(true);
}else{
验证码不正确"
4.图形验证码工具类
/**
*图形验证码工具类,目前主要是未登录,基于Cookie的方式。
用户已登录的情况,暂时没有。
*/
publicclassCaptchaCodeUtil{
privatestaticfinalLoggerlogger=LoggerFactory.getLogger(CaptchaCodeUtil.class);
publicstaticfinalStringCAPTCHA_NAMESPACE="
captcha"
;
//验证码在redis中保存的时间
publicstaticfinalintCAPTCHA_TIME_OUT=10*60;
//captcha_key为一个随机数,captchaCode为验证码
//captchaKey为redis中的key,captchaCode为value
//添加验证码信息到redis
publicstaticvoidaddCaptchaCode(Stringcaptcha_key,RedisServiceredisService,BizTypeEnumbizType,StringcaptchaCode)throwsBizException{
StringcaptchaKey=buildCaptchaKey(captcha_key,captchaCode,bizType);
if(StringUtils.isNotBlank(captchaKey)){
redisService.add(captchaKey,captchaCode,CAPTCHA_TIME_OUT,TimeUnit.SECONDS);
//验证某个业务的验证码,是否正确
publicstaticbooleancaptchaCodeOk(Stringcaptcha_key,RedisServiceredisService,BizTypeEnumbizType,StringcaptchaCode){
StringcaptchaKey=buildCaptchaKey(captcha_key,captchaCode,bizType);
if(StringUtils.isNotBlank(captchaKey)){
//验证码
StringcaptchaCodeRedis=redisService.getString(captchaKey);
if(StringUtils.equalsIgnoreCase(captchaCodeRedis,captchaCode)){
returntrue;
}catch(Exceptione){
logger.error(e.getMessage());
returnfalse;
//重新构建redis中的key
privatestaticStringbuildCaptchaKey(Stringcaptcha_key,StringcaptchaCode,BizTypeEnumbizType){
returnnull;
StringcaptchaKey=CAPTCHA_NAMESPACE+"
:
+bizType.getDir()+"
+captcha_key+"
+captchaCode;
returncaptchaKey;
//删除redis中验证码的信息
publicstaticvoiddeleteCaptchaCode(Stringcaptcha_key,RedisServiceredisService,StringcaptchaCode,BizTypeEnumbizType)throwsBizException{
redisService.delete(captchaKey);
5.登录和登出
@ApiOperation(value="
登录"
produces=MediaType.APPLICATION_JSON_VALUE)
/doLogin"
produces=MediaType.APPLICATION_JSON_VALUE)
doLogin(
@ApiParam(required=true,value="
登录名"
)@RequestParamStringloginName,
登录密码"
)@RequestParamStringpassword,
验证码"
)@RequestParamStringcaptchaCode,
HttpServletResponseresponse,
HttpServletRequestrequest
){
BizTypeEnumbizType=BizTypeEnum.LOGIN;
//从客户端cookie中得到redis的value
Stringcaptcha_Key=CookieUtil.getCookieValue(request,GlobalConst.BOM_CAPTCHA_KEY);
booleancaptchaCodeOk=CaptchaCodeUtil.captchaCodeOk(captcha_Key,redisService,bizType,captchaCode);
if(!
captchaCodeOk){
returnbuildBizErrorResult("
验证码错误!
//删除验证码
CaptchaCodeUtil.deleteCaptchaCode(captcha_Key,redisService,captchaCode,bizType);
Membermember=memberSecurityBizService.login(loginName,password);
if(member==null){
Loginfailed,loginName="
+loginName);
用户名和密码信息输入有误!
RoleTypeEnum.DESIGNER.getCode().equals(member.getRoleType())&
&
!
RoleTypeEnum.EDITOR.equals(member.getRoleType())){
你无权登录本系统!
//用户token的key值
Stringlogin_ut_key=UuidUtil.get32UUID();
//在客户端添加有关用户信息的Cookie
CookieUtil.addCookie(response,GlobalConst.BOM_FRONT_UT,login_ut_key,GlobalConst.KEEP_BOM_FRONT_UT_TIME);
//在redis中添加用户信息
redisService.add(login_ut_key,member,1,TimeUnit.HOURS);
returnbuildOk(true);
returnbuildExceptionResult(e);
登出"
produces=MediaType.TEXT_HTML_VALUE)
/logout"
publicStringlogout(HttpServletRequestrequest,HttpServletResponseresponse){
Logout"
StringaccessTokenKey=CookieUtil.getCookieValue(request,GlobalConst.BOM_FRONT_UT);
if(StringUtils.isNotBlank(accessTokenKey)){
CookieUtil.removeCookie(response,GlobalConst.BOM_FRONT_UT);
redisService.delete(accessTokenKey);
Logoutfailed"
return"
login/login"
6.登录拦截器LoginInterceptor
packagecom.front.interceptor;
importjava.io.IOException;
importjava.util.Map;
importjava.util.concurrent.TimeUnit;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importmons.lang3.StringUtils;
importorg.slf4j.Logger;
importorg.slf4j.LoggerFactory;
importorg.s
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 分布式 环境 中的 登录 应用