项目中的两个Controller层实现类,一个是跳转到jsp页面,一个是以Json形式返回Map键值对。
跳转到jsp页面:
1 package com.controller; 2 3 import java.io.IOException; 4 5 import javax.servlet.http.HttpServletRequest; 6 7 import org.apache.commons.httpclient.HttpClient; 8 import org.apache.commons.httpclient.HttpException; 9 import org.apache.commons.httpclient.methods.PostMethod; 10 import org.springframework.stereotype.Controller; 11 import org.springframework.web.bind.annotation.RequestMapping; 12 import org.springframework.web.servlet.ModelAndView; 13 14 import util.AuthConfig; 15 16 @Controller 17 public class Operate { 18 @RequestMapping(value="login.html") 19 public ModelAndView requestPost(HttpServletRequest request){ 20 AuthConfig config = AuthConfig.getInstance(); 21 String server = config.getConfigParameter("server"); 22 String client_id = config.getConfigParameter("client_id"); 23 String client_secret = config.getConfigParameter("client_secret"); 24 String redirect_uri = config.getConfigParameter("redirect_uri"); 25 String scope = config.getConfigParameter("scope"); 26 String url = server+"AuthServer/oauth2/authorize?client_id="+client_id+"&client_secret="+client_secret+"&redirect_uri="+redirect_uri+"&response_type=code&scope="+scope; 27 System.out.println(url); 28 ModelAndView mav = new ModelAndView("login"); 29 mav.addObject("url", url); 30 return mav; 31 } 32 }
一个是以Json形式返回Map键值对:
1 package com.bupt.auth.controller; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 import com.bupt.oauth2.token.AuthKeyGenerator; 7 import com.bupt.oauth2.token.DefaultAuthKeyGenerator; 8 import com.bupt.oauth2.utils.*; 9 10 import org.springframework.beans.factory.annotation.Autowired; 11 12 import com.bupt.oauth2.common.OAuth2AccessToken; 13 14 import org.springframework.web.bind.annotation.RequestBody; 15 import org.springframework.web.bind.annotation.RequestMapping; 16 import org.springframework.web.bind.annotation.RequestMethod; 17 import org.springframework.web.bind.annotation.ResponseBody; 18 import org.springframework.web.bind.annotation.RestController; 19 20 import com.bupt.auth.bussiness.RegisterAppBean; 21 import com.bupt.auth.bussiness.RegisterBean; 22 import com.bupt.auth.entity.User; 23 import com.bupt.auth.exception.MyException; 24 import com.bupt.auth.service.UserManager; 25 import com.bupt.oauth2.peve.TokenHandler; 26 import com.bupt.oauth2.common.util.OAuth2Utils; 27 import com.bupt.oauth2.common.util.VePeUtils; 28 29 @RestController 30 public class RegisteController 31 { 32 @Autowired 33 private TokenHandler tokenHandler; 34 @Autowired 35 private UserManager userManager; 36 37 private AuthKeyGenerator authKeyGenerator = new DefaultAuthKeyGenerator(); 38 39 @RequestMapping(value="/registeruser.html",method=RequestMethod.POST) 40 @ResponseBody 41 public Map<String, Object> register(@RequestBody RegisterBean registerbean) 42 { 43 Map<String, Object> map = new HashMap<String, Object>(); 44 User user = new User(); 45 try{ 46 if(userManager.findByUsername(registerbean.getUser_name()) != null){ 47 map.put("result", "fail"); 48 map.put("info", "Username has already exist!!"); 49 map.put("failcode", "602"); 50 return map; 51 } 52 }catch(MyException e){ 53 map.put("result", "fail"); 54 map.put("info", e.getMessage()); 55 map.put("failcode", e.getFailCode()); 56 return map; 57 } 58 59 user.setUsername(registerbean.getUser_name()); 60 user.setPassword(registerbean.getUser_password()); 61 user.setInfo(registerbean.getInfo()); 62 userManager.addUser(user); 63 64 createUserAccessToken(user); 65 System.out.println(registerbean.getUser_name()); 66 67 map.put("result", "success"); 68 return map; 69 } 70 71 private void createUserAccessToken(User user) 72 { 73 String userName = user.getUsername(); 74 String userId = String.valueOf(user.getId()); 75 Map<String, String> requestParameters = VePeUtils.buildRequestParameters(userId, null); 76 tokenHandler.accessToken(requestParameters); 77 //user 中存放authentication_id 78 //与JdbcTokenStore中storeAccessToken方法中生成authentication_id相对应 79 user.setAccesstoken(authKeyGenerator.extractKeyByVePeCredentials(user.toString())); 80 userManager.updateUser(user); 81 } 82 }
时间: 2024-10-03 13:46:34