package com.sldl.controller.app;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.sldl.model.app.AppUser;
import com.sldl.model.app.OaUser;
import com.sldl.service.app.RegisterAndLoginService;
/**
- 用户注册、校验、验证码、登录、退出、忘记密码接口
- @author lenovo
*/
@Controller
//设置允许跨域–所有端口
@RequestMapping("/oauser")
public class RegisterAndLoginController {
private static Logger log = Logger.getLogger(RegisterAndLoginController.class);
@Resource
RegisterAndLoginService service;
@RequestMapping("/getVerifyCode")
@ResponseBody
public JSON getVerifyCode(**HttpServletResponse response**,AppUser user){
**response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Headers", "content-type");
response.setHeader("Access-Control-request-method", "GET,POST");**
log.info("获取短信验证码接口");
return (JSON) JSON.toJSON(service.getVerifyCode(user));
}
/**
* 验证是否已注册并发验证码
* @param user
* @return
*/
@RequestMapping("/getVerifyCodeUser")
@ResponseBody
public JSON getVerifyCodeUser(HttpServletResponse response,AppUser user){
log.info("获取短信验证码接口");
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Headers", "content-type");
response.setHeader("Access-Control-request-method", "GET,POST");
return (JSON) JSON.toJSON(service.getVerifyCodeUser(user));
}
@RequestMapping("/compareVerifyCode")
@ResponseBody
public JSON compareVerifyCode(AppUser user){
log.info("校验短信验证码接口");
return (JSON) JSON.toJSON(service.compareVerifyCode(user));
}
/*@RequestMapping("/register")
@ResponseBody
public JSON register(HttpServletResponse response, AppUser user){
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Headers", "content-type");
response.setHeader("Access-Control-request-method", "GET,POST");
log.info("注册接口");
return (JSON) JSON.toJSON(service.register(user));
}*/
//忘记密码时发送验证码
@RequestMapping("/forgetPWD")
@ResponseBody
public JSON forgetPWD(HttpServletResponse response,AppUser user){
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Headers", "content-type");
response.setHeader("Access-Control-request-method", "GET,POST");
log.info("忘记密码时校验手机号是否存在并发送短信接口");
return (JSON) JSON.toJSON(service.getVerifyCodeWhenForgetPWD(user));
}
@RequestMapping("/resetPWD")
@ResponseBody
public JSON resetPWD(HttpServletResponse response,OaUser user){
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Headers", "content-type");
response.setHeader("Access-Control-request-method", "GET,POST");
log.info("修改密码");
return (JSON) JSON.toJSON(service.compareVerifyCodeAndResetPWD(user));
}
@RequestMapping("/login")
@ResponseBody
public JSON login(HttpServletRequest request,HttpServletResponse response,OaUser user){
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Headers", "content-type");
response.setHeader("Access-Control-request-method", "GET,POST");
log.info("登录接口");
return (JSON) JSON.toJSON(service.login(request,user));
}
@RequestMapping("/exit")
@ResponseBody
public JSON exit(HttpServletRequest request,HttpServletResponse response,OaUser user) {
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Headers", "content-type");
response.setHeader("Access-Control-request-method", "GET,POST");
log.info("退出登录接口");
service.exit(user);
Map<String,Object> map=new HashMap<>();
map.put("code", 0);
map.put("msg", "退出登录成功");
return (JSON) JSON.toJSON(map);
}
@RequestMapping("/getUserByToken")
@ResponseBody
public JSON getUserByToken(HttpServletRequest request,HttpServletResponse response,OaUser user){
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Headers", "content-type");
response.setHeader("Access-Control-request-method", "GET,POST");
log.info("获取用户信息接口");
return (JSON) JSON.toJSON(service.getUserByToken(user));
}
}
请看controller response的设置,通过本设置即可解决前后端接口访问的跨域问题。