1、自定义编写的配置sso.properties
## 认证服务器分配给应用的id
CLIENT_ID =10394a48238d5011d702abc6d09f57x
## 认证服务器分配给应用的secret
CLIENT_SECRET =71502b0bbd95d7a72d735d6818a3x
## oes认证服务器的地址
#String OES_URL = "http://ip:port/";
OES_URL =https://ip:port/
## 回调地址(转码后)
#String REDIRECT_URI = "http://ip:port/index.html/callback";
#本地测试
REDIRECT_URI =http://ip:port/wcxxzx/index
## 获取accesstoken的url后缀
OES_URL_ACCESSTOKEN_SUFFIX =api/oauth2/v1/access_token
## 获取服务器认证的url后缀
OES_URL_AUTH_SUFFIX =api/oauth2/v1/authorize
## 获取服务器退出的url后缀
OES_URL_LOGOUT_SUFFIX =api/oauth2/v1/logout
#本地测试REDIRECT_URI
URI =http://192.168.1.64:8000/wcxxzx/index.html
2、定义实体类
package com.zhjx.entity;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import lombok.Data;
import lombok.Setter;
/**
*
* @desc 读取sso配置文件
* @author zhjx-研发部-luobw
*
* 2019年3月15日
*/
@Data
@Configuration
@PropertySource("classpath:sso.properties")
@ConfigurationProperties(prefix="")
public class SSOConfig {
@Setter
private String CLIENT_ID;
// 认证服务器分配给应用的secret
@Setter
private String CLIENT_SECRET;
// oes认证服务器的地址
// String OES_URL = "http://10.47.202.218:28002/";
@Setter
private String OES_URL;// 本地测试
// 回调地址(转码后)
// String REDIRECT_URI = "http://10.41.148.24:8080/index.html/callback";
@Setter
private String REDIRECT_URI;// 本地测试
// 获取accesstoken的url后缀
@Setter
private String OES_URL_ACCESSTOKEN_SUFFIX;
// 获取服务器认证的url后缀
@Setter
private String OES_URL_AUTH_SUFFIX;
// 获取服务器退出的url后缀
@Setter
private String OES_URL_LOGOUT_SUFFIX;
//目标回调地址
@Setter
private String URI;// 本地测试
}
3、代码具体写法
/**
*
* @desc 认证服务器类
* @author zhjx-研发部-luobw
*
* 2019年3月13日
*/
@RestController
@EnableConfigurationProperties(SSOConfig.class)
public class LoginController {
@Value("${CLIENT_ID}")
private String CLIENT_ID;
// 认证服务器分配给应用的secret
@Value("${CLIENT_SECRET}")
private String CLIENT_SECRET;
// oes认证服务器的地址
// String OES_URL = "http://10.47.202.218:28002/";
@Value("${OES_URL}")
private String OES_URL;// 本地测试
// 回调地址(转码后)
// String REDIRECT_URI = "http://10.41.148.24:8080/index.html/callback";
@Value("${REDIRECT_URI}")
private String REDIRECT_URI;// 本地测试
// 获取accesstoken的url后缀
@Value("${OES_URL_ACCESSTOKEN_SUFFIX}")
private String OES_URL_ACCESSTOKEN_SUFFIX;
// 获取服务器认证的url后缀
@Value("${OES_URL_AUTH_SUFFIX}")
private String OES_URL_AUTH_SUFFIX;
// 获取服务器退出的url后缀
@Value("${OES_URL_LOGOUT_SUFFIX}")
private String OES_URL_LOGOUT_SUFFIX;
@Value("${URI}")
private String URI;// 本地测试
/**
*
* @desc @Api:修饰整个类,描述Controller的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiProperty:用对象接收参数时,描述对象的一个字段
@ApiResponse:HTTP响应其中1个描述
@ApiResponses:HTTP响应整体描述
@ApiIgnore:使用该注解忽略这个API
@ApiError :发生错误返回的信息
@ApiParamImplicitL:一个请求参数
@ApiParamsImplicit 多个请求参数
* @author zhjx-研发部-luobw
*
* @param code
* @param layer
* @param type
* @param cond
* @param response
* @return
* 2019年3月18日
*/
@ApiOperation("测试swagger")
@ApiImplicitParams({
@ApiImplicitParam(name="code",value = "自定义code"),
@ApiImplicitParam(name="layer",value = "区域层级"),
@ApiImplicitParam(name="codes",value = "网格码"),
@ApiImplicitParam(name="type",value = "请求类型,years按年统计,months按月统计,为空全部统计",dataType="String",paramType = "path",required = false)
})
@RequestMapping(value = "/sendRedirect", method = RequestMethod.GET)
public String sendRedirect(String code,String layer,String type,RedirectCallBackRequest cond,HttpServletResponse response) {
log.info("--------------测试---------");
try {
response.sendRedirect(REDIRECT_URI);//直接获取使用读取配置文件的key
} catch (IOException e) {
e.printStackTrace();
}
log.info("---------结束--------------");
return null;
}
}