Commit a3880ef8 authored by Quxl's avatar Quxl

x

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