项目中的两个Controller层实现类,一个是跳转到jsp页面,一个是以Json形式返回Map键值对。

跳转到jsp页面:

 package com.controller;

 import java.io.IOException;

 import javax.servlet.http.HttpServletRequest;

 import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import util.AuthConfig; @Controller
public class Operate {
@RequestMapping(value="login.html")
public ModelAndView requestPost(HttpServletRequest request){
AuthConfig config = AuthConfig.getInstance();
String server = config.getConfigParameter("server");
String client_id = config.getConfigParameter("client_id");
String client_secret = config.getConfigParameter("client_secret");
String redirect_uri = config.getConfigParameter("redirect_uri");
String scope = config.getConfigParameter("scope");
String url = server+"AuthServer/oauth2/authorize?client_id="+client_id+"&client_secret="+client_secret+"&redirect_uri="+redirect_uri+"&response_type=code&scope="+scope;
System.out.println(url);
ModelAndView mav = new ModelAndView("login");
mav.addObject("url", url);
return mav;
}
}

一个是以Json形式返回Map键值对:

 package com.bupt.auth.controller;

 import java.util.HashMap;
import java.util.Map; import com.bupt.oauth2.token.AuthKeyGenerator;
import com.bupt.oauth2.token.DefaultAuthKeyGenerator;
import com.bupt.oauth2.utils.*; import org.springframework.beans.factory.annotation.Autowired; import com.bupt.oauth2.common.OAuth2AccessToken; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController; import com.bupt.auth.bussiness.RegisterAppBean;
import com.bupt.auth.bussiness.RegisterBean;
import com.bupt.auth.entity.User;
import com.bupt.auth.exception.MyException;
import com.bupt.auth.service.UserManager;
import com.bupt.oauth2.peve.TokenHandler;
import com.bupt.oauth2.common.util.OAuth2Utils;
import com.bupt.oauth2.common.util.VePeUtils; @RestController
public class RegisteController
{
@Autowired
private TokenHandler tokenHandler;
@Autowired
private UserManager userManager; private AuthKeyGenerator authKeyGenerator = new DefaultAuthKeyGenerator(); @RequestMapping(value="/registeruser.html",method=RequestMethod.POST)
@ResponseBody
public Map<String, Object> register(@RequestBody RegisterBean registerbean)
{
Map<String, Object> map = new HashMap<String, Object>();
User user = new User();
try{
if(userManager.findByUsername(registerbean.getUser_name()) != null){
map.put("result", "fail");
map.put("info", "Username has already exist!!");
map.put("failcode", "602");
return map;
}
}catch(MyException e){
map.put("result", "fail");
map.put("info", e.getMessage());
map.put("failcode", e.getFailCode());
return map;
} user.setUsername(registerbean.getUser_name());
user.setPassword(registerbean.getUser_password());
user.setInfo(registerbean.getInfo());
userManager.addUser(user); createUserAccessToken(user);
System.out.println(registerbean.getUser_name()); map.put("result", "success");
return map;
} private void createUserAccessToken(User user)
{
String userName = user.getUsername();
String userId = String.valueOf(user.getId());
Map<String, String> requestParameters = VePeUtils.buildRequestParameters(userId, null);
tokenHandler.accessToken(requestParameters);
//user 中存放authentication_id
//与JdbcTokenStore中storeAccessToken方法中生成authentication_id相对应
user.setAccesstoken(authKeyGenerator.extractKeyByVePeCredentials(user.toString()));
userManager.updateUser(user);
}
}
04-14 02:39