package com.alatus.mall.auth.app; import com.alatus.common.constant.AuthServerConstant; import com.alatus.common.exception.BizCodeEnum; import com.alatus.common.utils.R; import com.alatus.mall.auth.feign.ThirdPartFeignService; import com.alatus.mall.auth.vo.UserRegisterVo; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; 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.validation.Valid; import java.util.Map; import java.util.UUID; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @Controller public class LoginController { @Autowired private ThirdPartFeignService thirdPartFeignService; @Autowired private StringRedisTemplate redisTemplate; @GetMapping("/sms/sendCode") @ResponseBody public R sendCode(@RequestParam("phone") String phone){ // TODO 接口防刷 // 防止反复刷验证码 String value = redisTemplate.opsForValue().get(AuthServerConstant.SMS_CODE_CACHE_PREFIX + phone); if(value!=null&&!StringUtils.isEmpty(value)){ long time = Long.parseLong(value.split("_")[1]); if(System.currentTimeMillis() - time < 60000){ // 60秒内不能再发 return R.error(BizCodeEnum.SMS_CODE_EXCEPTION.getCode(),BizCodeEnum.SMS_CODE_EXCEPTION.getMsg()); } } String code = UUID.randomUUID().toString().substring(0, 5); R sendCode = thirdPartFeignService.sendCode(phone, code); // 缓存验证码 redisTemplate.opsForValue().set(AuthServerConstant.SMS_CODE_CACHE_PREFIX+phone,code+"_"+System.currentTimeMillis(),10, TimeUnit.MINUTES); if(sendCode.getCode()==0){ return R.ok(); } else{ return R.error(); } } //RedirectAttributes redirectAttributes模拟重定向携带数据 // TODO 重定向携带数据,利用session原理,将数据放在session中 // 只要跳到下一个页面取出这个数据后,session的数据就被删掉了 // TODO 分布式Session问题解决!!! @PostMapping("/register") public String register(@Valid UserRegisterVo vo, BindingResult result, RedirectAttributes redirectAttributes){ if(result.hasErrors()){ // 收集错误信息返回 Map<String, String> collect = result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField,FieldError::getDefaultMessage)); redirectAttributes.addFlashAttribute("errors",collect); // 原请求是post请求,当我们失败以后,在这里转发,转发是会保持请求方式不变的,但是路径访问默认是get // 就会出现错误 // 原理是使用了HttpSession的底层实现 return "redirect:http://auth.alatusmall.com/reg.html"; } // 注册成功返回首页登录页 else{ return "login"; } } }
package com.alatus.mall.auth.app; import com.alatus.common.constant.AuthServerConstant; import com.alatus.common.exception.BizCodeEnum; import com.alatus.common.utils.R; import com.alatus.mall.auth.feign.ThirdPartFeignService; import com.alatus.mall.auth.vo.UserRegisterVo; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; 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.validation.Valid; import java.util.Map; import java.util.UUID; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @Controller public class LoginController { @Autowired private ThirdPartFeignService thirdPartFeignService; @Autowired private StringRedisTemplate redisTemplate; @GetMapping("/sms/sendCode") @ResponseBody public R sendCode(@RequestParam("phone") String phone){ // TODO 接口防刷 // 防止反复刷验证码 String value = redisTemplate.opsForValue().get(AuthServerConstant.SMS_CODE_CACHE_PREFIX + phone); if(value!=null&&!StringUtils.isEmpty(value)){ long time = Long.parseLong(value.split("_")[1]); if(System.currentTimeMillis() - time < 60000){ // 60秒内不能再发 return R.error(BizCodeEnum.SMS_CODE_EXCEPTION.getCode(),BizCodeEnum.SMS_CODE_EXCEPTION.getMsg()); } } String code = UUID.randomUUID().toString().substring(0, 5); R sendCode = thirdPartFeignService.sendCode(phone, code); // 缓存验证码 redisTemplate.opsForValue().set(AuthServerConstant.SMS_CODE_CACHE_PREFIX+phone,code+"_"+System.currentTimeMillis(),10, TimeUnit.MINUTES); if(sendCode.getCode()==0){ return R.ok(); } else{ return R.error(); } } //RedirectAttributes redirectAttributes模拟重定向携带数据 // TODO 重定向携带数据,利用session原理,将数据放在session中 // 只要跳到下一个页面取出这个数据后,session的数据就被删掉了 // TODO 分布式Session问题解决!!! @PostMapping("/register") public String register(@Valid UserRegisterVo vo, BindingResult result, RedirectAttributes redirectAttributes){ if(result.hasErrors()){ // 收集错误信息返回 Map<String, String> collect = result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField,FieldError::getDefaultMessage)); redirectAttributes.addFlashAttribute("errors",collect); // 原请求是post请求,当我们失败以后,在这里转发,转发是会保持请求方式不变的,但是路径访问默认是get // 就会出现错误 // 原理是使用了HttpSession的底层实现 return "redirect:http://auth.alatusmall.com/reg.html"; } // 注册成功返回首页登录页 else{ return "login"; } } }
商城项目后台使用RedirectAttributes实现重定向携带数据-----商城项目
最新推荐文章于 2024-10-06 20:16:20 发布