社å æ¥åã·ã¹ãã ã®WebAPIå®è£ ãèãã¦ã¿ã
ä»ã¾ã§ä»äºä¸ãæ¥åã·ã¹ãã ãéçºãã¦ãã¾ãããããã©ã¦ã¶ã§ã¢ã¯ã»ã¹ããWebã¢ããªã±ã¼ã·ã§ã³ã°ããã§ããã
ãã°ããã¯Webã¢ããªéçºãç¶ãã§ãããããããããã¿ãã¬ãã端æ«ãä¼ç¤¾ã§æ´»ç¨ããæµããåºã¦ããã®ã§ã
ãã£ããéçºããWebã¢ããªã«ã¿ãã¬ãã端æ«ããã§ãã¢ã¯ã»ã¹ã§ããããã«ãWebAPIã®å®è£
ã«ã¤ãã¦èãã¦ã¿ã¾ããã
ãªãjQueryMobileãªã©ã使ã£ãWebã¢ããªã«ããé¸æè¢ãããã¾ãããããã¯ããã¦JSON/JSONPãè¿ãWebAPIã®å®è£ ãèãã¾ãã
ä»æ§
- http/httpsã¢ã¯ã»ã¹ã§ãããã®ã¨ããã¬ã¹ãã³ã¹ã¯JSON/JSONPã§é¸æã§ããã
- æ¥åã·ã¹ãã ãªã®ã§ãèªè¨¼ãããã
- èªè¨¼ã¯ä¸åº¦è¡ã£ãããã°ã¢ã¦ããªã©ãããªãéãç¶ç¶ããã
- èªè¨¼é¨åã¯ãªãã¹ãç°¡åã«ç¬èªå®è£ ã
- ãã°ã¢ã¦ããå¯è½ã
- ã·ã¹ãã ã®å©ç¨ã¦ã¼ã¶ã¼ãå¤æ´ããå ´åã¯ãã°ã¢ã¦ããã¦åãã°ã¤ã³ã
- ã¨ã©ã¼ãçºçããå ´åã¯HTTPã¹ãã¼ã¿ã¹ã³ã¼ããè¿ãã
- ã¯ã©ã¤ã¢ã³ãéä¾åã
èªè¨¼ã®èãæ¹
èªè¨¼ã¯å½ç¶å¿
è¦ã§ããããã¨ãã°ã¿ãã¬ãã端æ«ã§å©ç¨ããå ´åã«ã
Webã¢ããªã¿ããã«å©ç¨ãã度ã«ãã°ã¤ã³ãå¿
è¦ã§ãä¸å®æéå©ç¨ããªãã¨ï¼ã»ãã·ã§ã³ã¿ã¤ã ã¢ã¦ãã«ããï¼åãã°ã¤ã³ãå¿
è¦ã»ã»ã»
ã¨ããã®ã¯ãã«ãæ°ãããã®ã§ãä¸åº¦ãã°ã¤ã³ããããã°ã¤ã³æ
å ±ããã£ã¨ç¶ç¶ãããããã«èãã¦ã¿ã¾ãã
ï¼ãã¡ã®ä¼ç¤¾ã§ã¯ã端æ«ã®ããã¯è§£é¤ãæ°å4æ¡ã§ã¯ãªãã¢ã«ãã¡ãããã¨æ°åãæ··ãã¦6æå以ä¸å¿
é ã¨ããããªã·ã¼ãããã®ã§ãããã«å ãã¦ã¢ããªã§ãèªè¨¼ãå¿
è¦ãªã®ã¯é¢åã§ã¯ãªããã¨ããèãã§ãï¼
ãã¡ããããã°ã¢ã¦ããããã¨ã¯å¯è½ã¨ãã¾ããããã°ã¤ã³æåããå ´åã«çºè¡ããããã¼ã¯ã³ããã°ã¢ã¦ãããã¨ç ´æ£ãããããã«ãã¾ãã
ã¢ããªã±ã¼ã·ã§ã³ãµã¼ãã¼ã®ã»ãã·ã§ã³æ©æ§ã使ã£ã¦ãåããã¨ã¯ã§ããããããã¾ãããã
èªåãªãã«ç´°ããã«ã¹ã¿ãã¤ãºãã§ããã»ããããããªã¨æããç¬èªå®è£
ãããã¨ã«ãã¾ãã
åºæ¬çãªèãæ¹ã¨ãã¦ã¯ã»ãã·ã§ã³IDã®ãããªãã¼ã¯ã³ãçºè¡ãããããã¦ã¼ã¶ã¼æ å ±ï¼ãã¼ã¿ãã¼ã¹ï¼ã«è¨é²ãã¾ãã
- ãã°ã¤ã³æåæã«ãã¼ã¯ã³ãçºè¡ãã以éã¯ãã®ãã¼ã¯ã³ãå ã«WebAPIã«ã¢ã¯ã»ã¹ãã¾ãããã¼ã¯ã³ã¯åè§ã¢ã«ãã¡ãããã¨æ°åã®ã©ã³ãã ã§30æåç¨åº¦ã¨ãã¾ãã
- ãã°ã¢ã¦ãããã¨ãã¼ã¯ã³ãç ´æ£ãã¾ãã
- ç¡å¹ãªãã¼ã¯ã³ã§ã¢ã¯ã»ã¹ã試ã¿ãã¨ãã¹ãã¼ã¿ã¹ã³ã¼ã401ãè¿ãã¾ãã
ããã°ã©ã ï¼SAStrutsï¼ã®ä¾
ããã§ã¯ãã¼ã¯ã³ããã°ã¤ã³ãã¼ï¼loginKeyï¼ã¨ãã¦ãã¾ãã
- WebAPIç¨Proxyã¯ã©ã¹
public class LoginCheckForWebAPINSProxy implements ActionProxy { @Resource protected HttpServletRequest request; @Resource protected HttpServletResponse response; @Resource protected UserService userService; @Override public String execute(ProxyChain proxyChain) throws Exception { String loginKey = request.getParameter("loginKey"); User user = userService.findValidUserByLoginKey(loginKey); if(user == null) { response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); return null; } return proxyChain.invoke(); } }
ãªã¯ã¨ã¹ããã©ã¡ã¼ã¿ã¼"loginKey"ã®å¤ãããã¼ã¿ãã¼ã¹ã®ã¦ã¼ã¶ã¼æ
å ±ãæ¤ç´¢ãã¾ãã
è¦ã¤ãããªãå ´åã¯ã¹ãã¼ã¿ã¹ã³ã¼ã401ãè¿ãã¾ãã
Proxyã¯ã©ã¹ã«ã¤ãã¦ã¯ãsastruts-extensionのActionProxyでログインチェック - おかひろの雑記ãè¦ã¦ä¸ããã
- ActionForm
// ãã°ã¤ã³ãã¼ public String loginKey; // ãã°ã¤ã³ID public String userId; // ãã¹ã¯ã¼ã public String password; // ã¬ã¹ãã³ã¹ãã©ã¼ããããjson/jsonp public String format; // jsonpã®å ´åã®ã³ã¼ã«ããã¯é¢æ°å public String callback; /** * jsonãjsonpããå¤æãã¦ã¬ã¹ãã³ã¹ãã¼ã¿ãçæãã * @param jsonData * @return */ public String generateJsonResponseString(String jsonData) { if(this.format != null && this.format.equalsIgnoreCase("jsonp")) { if(this.callback == null || this.callback.equals("")) { this.callback = "callback"; } return String.format("%s(%s);",this.callback,jsonData); } return jsonData; }
formatã«ãããjsonãjsonpããæå®ã§ããããã«ãã¾ããçç¥æã¯jsonã§ãã
ã¾ããjsonpã®å ´åã¯ã³ã¼ã«ããã¯é¢æ°åãæå®ã§ãã¾ãã
主ã«jsonpã®å ´åã«ã¬ã¹ãã³ã¹ãã¼ã¿ãçµã¿ç«ã¦ãã¡ã½ãããç¨æãã¦ãã¾ãã
- Action
/** * ãã°ã¤ã³ãã§ã㯠* @return */ @Execute(validator=false,urlPattern="login/{userId}/{password}") public String login() { User user = userService.findByUserIdPassword(form.userId,form.password); if(user != null) { // æ°ãããã°ã¤ã³ãã¼ãçºè¡ãã¦æ´æ° user.loginKey = userService.generateLoginKey(); userService.update(user); String responseJson = form.generateJsonResponseString(JSON.encode(user)); response.setHeader("Access-Control-Allow-Origin","*"); ResponseUtil.write(responseJson); } else { response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); } return null; } /** * ãã°ã¢ã¦ã * @return */ @Proxy(proxy=LoginCheckForWebAPINSProxy.class,type=ProxyType.OVERRIDE) @Execute(validator=false,urlPattern="logout/{loginKey}") public String logout() { // ãã°ã¤ã³ãã¼ãæ¶å» user user = userService.findValidUserByLoginKey(form.loginKey); user.loginKey = null; userService.update(user); response.setStatus(HttpServletResponse.SC_OK); return null; } /** * WebAPIã§å ¬éããæ©è½ * @return */ @Proxy(proxy=LoginCheckForWebAPINSProxy.class,type=ProxyType.OVERRIDE) @Execute(validator=false,urlPattern="hoge/{loginKey}") public String hoge() { ... String responseJson = form.generateJsonResponseString(JSON.encode(data)); response.setHeader("Access-Control-Allow-Origin","*"); ResponseUtil.write(responseJson); return null; }
ãã°ã¤ã³å¦çã§ã¯ãæå®ãããã¦ã¼ã¶ã¼IDã¨ãã¹ã¯ã¼ããæ£ãããã©ããããã§ãã¯ãã
æ£ãããã°ãã°ã¤ã³ãã¼ãçºè¡ãã¦ãã¼ã¿ãã¼ã¹ã«ä¿åãçµæã®æ
å ±ãJSONã«ãã¦è¿ãã¾ãã
ãã°ã¢ã¦ãå¦çã§ã¯ãæå®ããããã°ã¤ã³ãã¼ãæ£ãããã©ããããã§ãã¯ãã
æ£ãããã°ãã°ã¤ã³ãã¼ãç ´æ£ãã¾ãã
ç ´æ£ãããããã°ã¤ã³ãã¼ã¯ä½¿ããªããªãã®ã§ãåãã°ã¤ã³ãå¿
è¦ã«ãªãã¾ãã
WebAPIã§å ¬éããæ©è½ã«ã¯ãProxyãè¨å®ãã¾ããï¼èªè¨¼ãå¿ è¦ãªãæ©è½ã¯ä¸è¦ï¼
Access-Control-Allow-Originãããã¼ã¯ã主ã«Ajaxã§ã®ã¯ãã¹ãã¡ã¤ã³å¯¾å¿ã§ãã
ï¼jsonpãããã§ããã»ã»ã»ï¼
ã¾ã¨ã
æ¥åã·ã¹ãã ã¨ãã£ã¦ãããããããã®ã§ãèªè¨¼ã®é¨åã¯æ±ããããã»ãã¥ãªãã£è¦ä»¶ãå¤ãã£ã¦ããã¨æãã¾ãã
å¿
è¦ã«å¿ãã¦ããã¼ã¯ã³ã®æå¹æéãè¨ããããIPã¢ãã¬ã¹ãUser-Agentã«ãããã£ã«ã¿ã¼ããããï¼ãã¾ãç¨åº¦ããããã¾ãããï¼ããã¨ãããã¨æãã¾ãã
ãã£ã¨èãã¤ããç¨åº¦ã®ãã®ã§ãããã¡ã¢ã¨ãã¦æ®ãã¦ããã¾ãã
ãã£ã¨è¯ãæ¹æ³ãããã°ãæãã¦ããã ãããå¬ããã§ãã