Commit a3880ef8 authored by Quxl's avatar Quxl

x

parent 329817e3
...@@ -33,51 +33,52 @@ import com.alibaba.fastjson.JSONObject; ...@@ -33,51 +33,52 @@ import com.alibaba.fastjson.JSONObject;
* <p>以下为接口实现案例</p> * <p>以下为接口实现案例</p>
* <p>Example():</p> * <p>Example():</p>
* <pre> * <pre>
public class OAuthApiImpl implements OAuthApi { * <span>@</span>Component
* public class OAuthApiImpl implements OAuthApi {
private OAuthConfig config; *
* private OAuthConfig config;
<span>@</span>Autowired *
HttpServletRequest request; * <span>@</span>Autowired
* HttpServletRequest request;
<span>@</span>Autowired *
HttpServletResponse response; * <span>@</span>Autowired
* HttpServletResponse response;
*
<span>@</span>Value("${client_id}") private String clientId; *
<span>@</span>Value("${client_secret}") private String clientSecret; * <span>@</span>Value("${client_id}") private String clientId;
<span>@</span>Value("${callback_url}") private String callbackUrl; * <span>@</span>Value("${client_secret}") private String clientSecret;
<span>@</span>Value("${authorize_url}") private String authorizeUrl; * <span>@</span>Value("${callback_url}") private String callbackUrl;
<span>@</span>Value("${access_token_url}") private String accessTokenUrl; * <span>@</span>Value("${authorize_url}") private String authorizeUrl;
* <span>@</span>Value("${access_token_url}") private String accessTokenUrl;
<span>@</span>Override *
public OAuthConfig getOAuthConfig() { * <span>@</span>Override
if(config == null) { * public OAuthConfig getOAuthConfig() {
synchronized (this) { * if(config == null) {
if(config == null) { * synchronized (this) {
config = new OAuthConfig(); * if(config == null) {
config.setAccessTokenUrl(accessTokenUrl); * config = new OAuthConfig();
config.setAuthorizeUrl(authorizeUrl); * config.setAccessTokenUrl(accessTokenUrl);
config.setCallbackUrl(callbackUrl); * config.setAuthorizeUrl(authorizeUrl);
config.setClientId(clientId); * config.setCallbackUrl(callbackUrl);
config.setClientSecret(clientSecret); * config.setClientId(clientId);
} * config.setClientSecret(clientSecret);
} * }
} * }
return config; * }
} * return config;
* }
<span>@</span>Override *
public HttpServletRequest getHttpServletRequest() { * <span>@</span>Override
return request; * public HttpServletRequest getHttpServletRequest() {
} * return request;
* }
<span>@</span>Override *
public HttpServletResponse getHttpServletResponse() { * <span>@</span>Override
return response; * public HttpServletResponse getHttpServletResponse() {
} * return response;
* }
} *
* }
* </pre> * </pre>
* *
* *
...@@ -86,25 +87,31 @@ import com.alibaba.fastjson.JSONObject; ...@@ -86,25 +87,31 @@ import com.alibaba.fastjson.JSONObject;
* <p>以下为接口调用案例</p> * <p>以下为接口调用案例</p>
* <p>Example():</p> * <p>Example():</p>
* <pre> * <pre>
* <span>@</span>Controller
* <span>@</span>RequestMapping
* public class LoginController { * public class LoginController {
* *
* <span>@</span>Autowired
* OAuthApi oAuthApi; * OAuthApi oAuthApi;
* *
* <span>@</span>GetMapping
* public ModelAndView index() { * public ModelAndView index() {
* OAuthConfig config = oAuthApi.getOAuthConfig(); * OAuthConfig config = oAuthApi.getOAuthConfig();
* ModelAndView mav = new ModelAndView(); * ModelAndView mav = new ModelAndView();
* mav.addObject("loginUrl", oAuthApi.getAuthorizationUrl()); //设置登陆授权URL, 用户可以在页面点击此URL,进入OAuth系统授权 * mav.addObject("loginUrl", oAuthApi.getAuthorizationUrl());
* mav.addObject("config", config); * mav.addObject("config", config);
* mav.setViewName("index.html"); * mav.setViewName("index.html");
* return mav; * return mav;
* } * }
* *
* <span>@</span>GetMapping("toLogin")
* public void toLogin() { * public void toLogin() {
* oAuthApi.doRedirectOAuthLogin(); //直接转到OAuth系统授权,和用户主动在页面点击授权URL效果一样,都是跳转到一样的地址 * oAuthApi.doRedirectOAuthLogin();
* } * }
* *
* <span>@</span>RequestMapping(value="callback", method= {RequestMethod.GET, RequestMethod.POST})
* public ModelAndView doLogin() { * public ModelAndView doLogin() {
* oAuthApi.callback(); //授权成功后,授权系统通过重定向的方式回调业务系统,并附带授权参数信息,此代码用于解析授权系统的回调参数 * oAuthApi.callback();
* OAuthToken token = oAuthApi.getOAuthToken(); * OAuthToken token = oAuthApi.getOAuthToken();
* ModelAndView mav = new ModelAndView(); * ModelAndView mav = new ModelAndView();
* mav.addObject("token", token); * mav.addObject("token", token);
...@@ -112,6 +119,7 @@ import com.alibaba.fastjson.JSONObject; ...@@ -112,6 +119,7 @@ import com.alibaba.fastjson.JSONObject;
* return mav; * return mav;
* } * }
* *
* <span>@</span>RequestMapping(value="doLogout", method= {RequestMethod.GET, RequestMethod.POST})
* public ModelAndView doLogout(HttpServletResponse response) throws IOException { * public ModelAndView doLogout(HttpServletResponse response) throws IOException {
* oAuthApi.removeOAuthToken(); * oAuthApi.removeOAuthToken();
* ModelAndView mav = new ModelAndView(); * ModelAndView mav = new ModelAndView();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment