1舰蟆、集成Oauth2,pom增加依賴:
<dependency>
? ? ? ? ? ? <groupId>org.springframework.cloud</groupId>
? ? ? ? ? ? <artifactId>spring-cloud-starter-oauth2</artifactId>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework.cloud</groupId>
? ? ? ? ? ? <artifactId>spring-cloud-starter-openfeign</artifactId>
? ? ? ? </dependency>
1狸棍、增加會話控制處理接口及實現(xiàn)類:
public interface PermissionService {
/**
*
* @Title: hasPermission??
* @Description: 請求鑒權
* @param @param request
* @param @param authentication
* @param @return??
* @return boolean?
* @throws??
*/
boolean hasPermission(HttpServletRequest request, Authentication authentication);
}
@Service("permissionService")
public class PermissionServiceImpl implements PermissionService {
private static Logger log = LoggerFactory.getLogger(PermissionServiceImpl.class);
/**
* 判斷請求是否有權限
*
* @param request? ? ? ? HttpServletRequest
* @param authentication 認證信息
* @return 是否有權限
*/
public boolean hasPermission(HttpServletRequest request, Authentication authentication) {
log.info("check request permission ==> " + request.getRequestURI());
/**
* 以下代碼僅供本地開發(fā)聯(lián)調時打開身害,生產打包注釋掉這塊代碼
* 解決本地開發(fā)聯(lián)調時跨域問題訪問不到圖片的問題,本地開發(fā)聯(lián)調時隔缀,前端ajax訪問靜態(tài)文件流存在跨域OPTIONS請求题造,如果OPTIONS不被允許的話前端則不會發(fā)起真正的資源請求
* OPTIONS請求不帶cookie,所以不能進行會話檢查
*/
// ? ? Enumeration<String> names = request.getHeaderNames();
// ? ? log.info("===================================================================");
// ? ? ? while(names.hasMoreElements()){
// ? ? ? ? String name = (String) names.nextElement();
// ? ? ? ? log.info(name + ":" + request.getHeader(name));
// ? ? ? }
// ? ? log.info("===================================================================");
// ? ?
// ? ? if("OPTIONS".equals(request.getMethod())) {
// ? ? log.info("===================OPTIONS=====================================");
// ? ? return true;
// ? ? }
//校驗會話
//獲取請求Set-Cookie中的JSESSIONID的值
? ? Cookie[] cookies = request.getCookies();
? ? String sessionId = null;
? ? if(cookies != null) {
? ? for(Cookie cookie : cookies) {
? ? if("JSESSIONID".equals(cookie.getName())) {
? ? sessionId = cookie.getValue();
? ? break;
? ? }
? ? }
? ? }
? ? //從redis獲取會話
? ? HttpSession session = new HttpSession();
? ? session.setSessionId(sessionId);
? ? if(!session.isAvailable()) {
? ? log.info("會話超時猾瘸,請重新登錄");
? ? ? ? return false;
? ? }
return true;
}
}
3、將會話控制處理類配置到Oauth2中,新建配置類繼承ResourceServerConfigurerAdapter
@Configuration
@EnableResourceServer
public class ResourceConfigurerAdapter extends ResourceServerConfigurerAdapter {
@Autowired
? ? private OAuth2WebSecurityExpressionHandler expressionHandler;
@Override
? ? public void configure(HttpSecurity http) throws Exception {
? ? ? ? http.headers().frameOptions().disable();
? ? ? ? http
? ? ? ? ? ? .authorizeRequests()
? ? ? ? ? ? .antMatchers("/auth/oauth/token_key").denyAll()
? ? ? ? ? ? .antMatchers(
? ? ? ? ? ? //不做鑒權的URL
? ? ? ? ? ? "/code/image",
? ? ? ? ? ? "/admin/appInfo/query"
? ? ? ? ? ? ).permitAll()
? ? ? ? ? ? .anyRequest()
? ? ? ? ? ? .access("@permissionService.hasPermission(request, authentication)")
? ? ? ? ? ? .and()
? ? ? ? ? ? .csrf().disable();//允許跨域訪問
? ? }
@Override
? ? public void configure(ResourceServerSecurityConfigurer resources) {
? ? ? ? resources.expressionHandler(expressionHandler);
? ? }
? ? /**
? ? * 異常問題
? ? * #oauth2.throwOnError(@permissionService.hasPermission(request, authentication))
? ? * #EL1057E: No bean resolver registered in the context to resolve access to bean 'permissionService'
? ? *
? ? * @param applicationContext ApplicationContext
? ? * @return OAuth2WebSecurityExpressionHandler
? ? */
? ? @Bean
? ? public OAuth2WebSecurityExpressionHandler oAuth2WebSecurityExpressionHandler(ApplicationContext applicationContext) {
? ? ? ? OAuth2WebSecurityExpressionHandler expressionHandler = new OAuth2WebSecurityExpressionHandler();
? ? ? ? expressionHandler.setApplicationContext(applicationContext);
? ? ? ? return expressionHandler;
? ? }
}