• Controller层的写法


    项目中的两个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 }
  • 相关阅读:
    【Thymeleaf】遇到Current request is not a multipart request不要慌,检查页面中form的属性enctype="multipart/formdata"是否正确就对了
    [CSS]让ul中的li在所属div内成一行居中显示。
    【JS】将yyyyMMdd hh:mm:ss的字符串时间转换为JS时间
    分页资料收集
    【oralce/springboot/jquery】自行实现分页
    【JS】一小时之内显示红饼图标,两小时之内选择黄星图标,否则显示时间
    【JavaScript】给动态生成的链接动态的绑定事件
    VBA在Excel中的应用(四)
    在MOSS 2007中自定义DataView Webpart的数据显示样式
    ASP.NET中的缩略图应用
  • 原文地址:https://www.cnblogs.com/godlei/p/5615509.html
Copyright © 2020-2023  润新知