Skip to content

Commit

Permalink
feat: 认证服务代码初始化
Browse files Browse the repository at this point in the history
  • Loading branch information
zsy0216 committed Apr 22, 2021
1 parent d73f0cc commit c21b6a3
Show file tree
Hide file tree
Showing 115 changed files with 118,249 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
package com.zsy.auth.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;
*/
/**
* @Description: springSession配置类
* @Created: with IntelliJ IDEA.
* @author: 夏沫止水
* @createTime: 2020-06-29 13:36
**//*
@Configuration
public class GulimallSessionConfig {
@Bean
public CookieSerializer cookieSerializer() {
DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();
//放大作用域
cookieSerializer.setDomainName("gulimall.com");
cookieSerializer.setCookieName("GULISESSION");
return cookieSerializer;
}
@Bean
public RedisSerializer<Object> springSessionDefaultRedisSerializer() {
return new GenericJackson2JsonRedisSerializer();
}
}
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
package com.zsy.auth.config;
import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler;
import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import com.xunqi.common.exception.BizCodeEnum;
import com.xunqi.common.utils.R;
import org.springframework.context.annotation.Configuration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
*/
/**
* @author: zhangshuaiyin
* @create: 2021-04-22 20:19
**//*
@Configuration
public class MallAuthServerSentinelConfig {
public MallAuthServerSentinelConfig() {
WebCallbackManager.setUrlBlockHandler(new UrlBlockHandler() {
@Override
public void blocked(HttpServletRequest request, HttpServletResponse response, BlockException ex) throws IOException {
R error = R.error(BizCodeEnum.TO_MANY_REQUEST.getCode(), BizCodeEnum.TO_MANY_REQUEST.getMessage());
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json");
response.getWriter().write(JSON.toJSONString(error));
}
});
}
}
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.zsy.auth.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
* @author: zhangshuaiyin
* @createTime: 2021-04-22 08:52
**/
@Configuration
public class MallWebConfig implements WebMvcConfigurer {

/**·
* 视图映射:发送一个请求,直接跳转到一个页面
* @param registry
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {

// registry.addViewController("/login.html").setViewName("login");
registry.addViewController("/reg.html").setViewName("reg");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package com.zsy.auth.controller;

import com.alibaba.fastjson.TypeReference;
import com.zsy.auth.feign.MemberFeignService;
import com.zsy.auth.feign.ThirdPartFeignService;
import com.zsy.common.utils.R;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
* @author: zhangshuaiyin
* @createTime: 2020-06-24 10:37
**/

@Controller
public class LoginController {

@Autowired
private ThirdPartFeignService thirdPartFeignService;

@Autowired
private MemberFeignService memberFeignService;

// @Autowired
// private StringRedisTemplate stringRedisTemplate;

// @ResponseBody
// @GetMapping(value = "/sms/sendCode")
// public R sendCode(@RequestParam("phone") String phone) {
//
// //1、接口防刷
// String redisCode = stringRedisTemplate.opsForValue().get(AuthServerConstant.SMS_CODE_CACHE_PREFIX + phone);
// if (!StringUtils.isEmpty(redisCode)) {
// //活动存入redis的时间,用当前时间减去存入redis的时间,判断用户手机号是否在60s内发送验证码
// long currentTime = Long.parseLong(redisCode.split("_")[1]);
// if (System.currentTimeMillis() - currentTime < 60000) {
// //60s内不能再发
// return R.error(BizCodeEnum.SMS_CODE_EXCEPTION.getCode(),BizCodeEnum.SMS_CODE_EXCEPTION.getMessage());
// }
// }
//
// //2、验证码的再次效验 redis.存key-phone,value-code
// int code = (int) ((Math.random() * 9 + 1) * 100000);
// String codeNum = String.valueOf(code);
// String redisStorage = codeNum + "_" + System.currentTimeMillis();
//
// //存入redis,防止同一个手机号在60秒内再次发送验证码
// stringRedisTemplate.opsForValue().set(AuthServerConstant.SMS_CODE_CACHE_PREFIX+phone,
// redisStorage,10, TimeUnit.MINUTES);
//
// thirdPartFeignService.sendCode(phone, codeNum);
//
// return R.ok();
// }


/**
*
* TODO: 重定向携带数据:利用session原理,将数据放在session中。
* TODO:只要跳转到下一个页面取出这个数据以后,session里面的数据就会删掉
* TODO:分布下session问题
* RedirectAttributes:重定向也可以保留数据,不会丢失
* 用户注册
* @return
*/
// @PostMapping(value = "/register")
// public String register(@Valid UserRegisterVo vos, BindingResult result,
// RedirectAttributes attributes) {
//
// //如果有错误回到注册页面
// if (result.hasErrors()) {
// Map<String, String> errors = result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
// attributes.addFlashAttribute("errors",errors);
//
// //效验出错回到注册页面
// return "redirect:http://auth.gulimall.com/reg.html";
// }
//
// //1、效验验证码
// String code = vos.getCode();
//
// //获取存入Redis里的验证码
// String redisCode = stringRedisTemplate.opsForValue().get(AuthServerConstant.SMS_CODE_CACHE_PREFIX + vos.getPhone());
// if (!StringUtils.isEmpty(redisCode)) {
// //截取字符串
// if (code.equals(redisCode.split("_")[0])) {
// //删除验证码;令牌机制
// stringRedisTemplate.delete(AuthServerConstant.SMS_CODE_CACHE_PREFIX+vos.getPhone());
// //验证码通过,真正注册,调用远程服务进行注册
// R register = memberFeignService.register(vos);
// if (register.getCode() == 0) {
// //成功
// return "redirect:http://auth.gulimall.com/login.html";
// } else {
// //失败
// Map<String, String> errors = new HashMap<>();
// errors.put("msg", register.getData("msg",new TypeReference<String>(){}));
// attributes.addFlashAttribute("errors",errors);
// return "redirect:http://auth.gulimall.com/reg.html";
// }
//
//
// } else {
// //效验出错回到注册页面
// Map<String, String> errors = new HashMap<>();
// errors.put("code","验证码错误");
// attributes.addFlashAttribute("errors",errors);
// return "redirect:http://auth.gulimall.com/reg.html";
// }
// } else {
// //效验出错回到注册页面
// Map<String, String> errors = new HashMap<>();
// errors.put("code","验证码错误");
// attributes.addFlashAttribute("errors",errors);
// return "redirect:http://auth.gulimall.com/reg.html";
// }
// }


@GetMapping(value = "/login.html")
public String loginPage(HttpSession session) {

//从session先取出来用户的信息,判断用户是否已经登录过了
// Object attribute = session.getAttribute(LOGIN_USER);
//如果用户没登录那就跳转到登录页面
// if (attribute == null) {
return "login";
// } else {
// return "redirect:http://gulimall.com";
// }

}


// @PostMapping(value = "/login")
// public String login(UserLoginVo vo, RedirectAttributes attributes, HttpSession session) {
//
// //远程登录
// R login = memberFeignService.login(vo);
//
// if (login.getCode() == 0) {
// MemberResponseVo data = login.getData("data", new TypeReference<MemberResponseVo>() {});
// session.setAttribute(LOGIN_USER,data);
// return "redirect:http://gulimall.com";
// } else {
// Map<String,String> errors = new HashMap<>();
// errors.put("msg",login.getData("msg",new TypeReference<String>(){}));
// attributes.addFlashAttribute("errors",errors);
// return "redirect:http://auth.gulimall.com/login.html";
// }
// }


// @GetMapping(value = "/loguot.html")
// public String logout(HttpServletRequest request) {
// request.getSession().removeAttribute(LOGIN_USER);
// request.getSession().invalidate();
// return "redirect:http://gulimall.com";
// }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// package com.zsy.auth.controller;
//
// import com.alibaba.fastjson.JSON;
// import com.zsy.auth.feign.MemberFeignService;
// import com.zsy.auth.vo.SocialUser;
// import lombok.extern.slf4j.Slf4j;
// import org.apache.http.HttpResponse;
// import org.apache.http.util.EntityUtils;
// import org.springframework.beans.factory.annotation.Autowired;
// import org.springframework.stereotype.Controller;
// import org.springframework.web.bind.annotation.GetMapping;
// import org.springframework.web.bind.annotation.RequestParam;
//
// import javax.servlet.http.HttpSession;
// import java.util.HashMap;
// import java.util.Map;
//
// /**
// * @Description: 处理社交登录请求
// * @Created: with IntelliJ IDEA.
// * @author: 夏沫止水
// * @createTime: 2020-06-28 10:16
// **/
// @Slf4j
// @Controller
// public class OAuth2Controller {
//
// @Autowired
// private MemberFeignService memberFeignService;
//
//
// @GetMapping(value = "/oauth2.0/weibo/success")
// public String weibo(@RequestParam("code") String code, HttpSession session) throws Exception {
//
// Map<String, String> map = new HashMap<>();
// map.put("client_id","2077705774");
// map.put("client_secret","40af02bd1c7e435ba6a6e9cd3bf799fd");
// map.put("grant_type","authorization_code");
// map.put("redirect_uri","http://auth.gulimall.com/oauth2.0/weibo/success");
// map.put("code",code);
//
// //1、根据用户授权返回的code换取access_token
// HttpResponse response = HttpUtils.doPost("https://api.weibo.com", "/oauth2/access_token", "post", new HashMap<>(), map, new HashMap<>());
//
// //2、处理
// if (response.getStatusLine().getStatusCode() == 200) {
// //获取到了access_token,转为通用社交登录对象
// String json = EntityUtils.toString(response.getEntity());
// //String json = JSON.toJSONString(response.getEntity());
// SocialUser socialUser = JSON.parseObject(json, SocialUser.class);
//
// //知道了哪个社交用户
// //1)、当前用户如果是第一次进网站,自动注册进来(为当前社交用户生成一个会员信息,以后这个社交账号就对应指定的会员)
// //登录或者注册这个社交用户
// System.out.println(socialUser.getAccess_token());
// //调用远程服务
// R oauthLogin = memberFeignService.oauthLogin(socialUser);
// if (oauthLogin.getCode() == 0) {
// MemberResponseVo data = oauthLogin.getData("data", new TypeReference<MemberResponseVo>() {});
// log.info("登录成功:用户信息:{}",data.toString());
//
// //1、第一次使用session,命令浏览器保存卡号,JSESSIONID这个cookie
// //以后浏览器访问哪个网站就会带上这个网站的cookie
// //TODO 1、默认发的令牌。当前域(解决子域session共享问题)
// //TODO 2、使用JSON的序列化方式来序列化对象到Redis中
// session.setAttribute(LOGIN_USER,data);
//
// //2、登录成功跳回首页
// return "redirect:http://gulimall.com";
// } else {
//
// return "redirect:http://auth.gulimall.com/login.html";
// }
//
// } else {
// return "redirect:http://auth.gulimall.com/login.html";
// }
//
// }
//
// }
Loading

0 comments on commit c21b6a3

Please sign in to comment.