自定义Caslogout

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<context:annotation-config/>

<bean id="root" class="org.restlet.ext.spring.SpringRouter">
<property name="attachments">
<map>
<entry key="/tickets">
<bean class="org.restlet.ext.spring.SpringFinder">
<lookup-method name="createResource" bean="ticketResource" />
</bean>
</entry>
<entry key="/tickets/{ticketGrantingTicketId}">
<bean class="org.restlet.ext.spring.SpringFinder">
<lookup-method name="createResource" bean="ticketGrantingTicketResource" />
</bean>
</entry>
<entry key="/tickets/remove/{ticketGrantingTicketId}">
<bean class="org.restlet.ext.spring.SpringFinder">
<lookup-method name="createResource" bean="RemoveTicketGranting" />
</bean>
</entry>
</map>
</property>
</bean>

<bean id="ticketResource" class="org.jasig.cas.integration.restlet.TicketResource" scope="prototype" />

<bean id="ticketGrantingTicketResource" class="org.jasig.cas.integration.restlet.TicketGrantingTicketResource" scope="prototype"
p:httpClient-ref="httpClient"/>
<bean id="RemoveTicketGranting" class="org.jasig.cas.integration.restlet.RemoveTicketGranting" scope="prototype"
p:httpClient-ref="httpClient"/>

</beans>

package org.jasig.cas.integration.restlet;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jasig.cas.CentralAuthenticationService;
import org.jasig.cas.authentication.principal.SimpleWebApplicationServiceImpl;
import org.jasig.cas.ticket.InvalidTicketException;
import org.jasig.cas.util.HttpClient;
import org.jasig.cas.web.support.CookieRetrievingCookieGenerator;
import org.restlet.Context;
import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.data.Status;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.ResourceException;
import org.restlet.resource.Variant;
import org.springframework.beans.factory.annotation.Autowired;

public class RemoveTicketGranting extends Resource{
private final static Log log = LogFactory.getLog(RemoveTicketGranting.class);

@Autowired
private CentralAuthenticationService centralAuthenticationService;
/** CookieGenerator for TGT Cookie */
@Autowired
private CookieRetrievingCookieGenerator ticketGrantingTicketCookieGenerator;

/** CookieGenerator for Warn Cookie */
@Autowired
private CookieRetrievingCookieGenerator warnCookieGenerator;

private String ticketGrantingTicketId;

@Autowired
@NotNull
private HttpClient httpClient;

public void init(final Context context, final Request request, final Response response) {
super.init(context, request, response);
this.ticketGrantingTicketId = (String) request.getAttributes().get("ticketGrantingTicketId");

// this.centralAuthenticationService
// .destroyTicketGrantingTicket(ticketGrantingTicketId);
//
// this.ticketGrantingTicketCookieGenerator.removeCookie((HttpServletResponse) response);
// this.warnCookieGenerator.removeCookie((HttpServletResponse) response);

this.getVariants().add(new Variant(MediaType.APPLICATION_WWW_FORM));
}

public boolean allowDelete() {
return true;
}

public boolean allowPost() {
return true;
}

public void setHttpClient(final HttpClient httpClient) {
this.httpClient = httpClient;
}
public void removeRepresentations() throws ResourceException {
this.centralAuthenticationService.destroyTicketGrantingTicket(this.ticketGrantingTicketId);
getResponse().setStatus(Status.SUCCESS_OK);
}

public void acceptRepresentation(final Representation entity)
throws ResourceException {
this.centralAuthenticationService.destroyTicketGrantingTicket(this.ticketGrantingTicketId);
getResponse().setStatus(Status.SUCCESS_OK);
getResponse().setEntity("REMOVE_OK", MediaType.TEXT_PLAIN);
}
}

package org.jasig.cas.util;

import org.jasig.cas.Exception.LDCAuthenticationException;
import org.jasig.cas.authentication.handler.support.AbstractUsernamePasswordAuthenticationHandler;
import org.jasig.cas.authentication.principal.UsernamePasswordCredentials;
import org.springframework.util.StringUtils;

public class LDCUserAuthenticationHandler extends AbstractUsernamePasswordAuthenticationHandler{

@Override
protected boolean authenticateUsernamePasswordInternal(UsernamePasswordCredentials credentials)
throws LDCAuthenticationException {

final String username = credentials.getUsername();
final String password = credentials.getPassword();

if (StringUtils.hasText(username) && StringUtils.hasText(password)
&& username.equals(getPasswordEncoder().encode(password))) {
log
.debug("User [" + username
+ "] was successfully authenticated.");
return true;
}

log.debug("User [" + username + "] failed authentication");

return false;
}

}

package com.wolf.eureka.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Map;
import java.util.Map.Entry;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

/**
* https 请求 微信为https的请求
*
*/
public class HttpKit {

private static final String DEFAULT_CHARSET = "UTF-8"; // 默认字符集

private static final String _GET = "GET"; // GET
private static final String _POST = "POST";// POST
private static final String _DELETE = "Delete ";// DELETE Delete

/**
* 初始化http请求参数
*
* @param url
* @param method
* @param headers
* @return
* @throws IOException
*/
private static HttpURLConnection initHttp(String url, String method, Map<String, String> headers)
throws IOException {
URL _url = new URL(url);
HttpURLConnection http = (HttpURLConnection) _url.openConnection();
// 连接超时
http.setConnectTimeout(25000);
// 读取超时 --服务器响应比较慢,增大时间
http.setReadTimeout(25000);
http.setRequestMethod(method);
http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
http.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36");
if (null != headers && !headers.isEmpty()) {
for (Entry<String, String> entry : headers.entrySet()) {
http.setRequestProperty(entry.getKey(), entry.getValue());
}
}
http.setDoOutput(true);
http.setDoInput(true);
http.connect();
return http;
}

/**
* 初始化http请求参数
*
* @param url
* @param method
* @return
* @throws IOException
* @throws NoSuchAlgorithmException
* @throws NoSuchProviderException
* @throws KeyManagementException
*/
private static HttpsURLConnection initHttps(String url, String method, Map<String, String> headers)
throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL _url = new URL(url);
HttpsURLConnection http = (HttpsURLConnection) _url.openConnection();
// 设置域名校验
http.setHostnameVerifier(new HttpKit().new TrustAnyHostnameVerifier());
// 连接超时
http.setConnectTimeout(25000);
// 读取超时 --服务器响应比较慢,增大时间
http.setReadTimeout(25000);
http.setDoOutput(true);
http.setRequestMethod(method);
http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
http.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36");
if (null != headers && !headers.isEmpty()) {
for (Entry<String, String> entry : headers.entrySet()) {
http.setRequestProperty(entry.getKey(), entry.getValue());
}
}
http.setSSLSocketFactory(ssf);
http.setDoOutput(true);
http.setDoInput(true);
http.connect();
return http;
}

/**
*
* @description 功能描述: get 请求
* @return 返回类型:
*/
public static String get(String url, Map<String, String> params, Map<String, String> headers) {
StringBuffer bufferRes = null;
try {
HttpURLConnection http = null;
if (isHttps(url)) {
http = initHttps(initParams(url, params), _GET, headers);
} else {
http = initHttp(initParams(url, params), _GET, headers);
}
InputStream in = http.getInputStream();
BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));
String valueString = null;
bufferRes = new StringBuffer();
while ((valueString = read.readLine()) != null) {
bufferRes.append(valueString);
}
read.close();
in.close();
if (http != null) {
http.disconnect();// 关闭连接
}
return bufferRes.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

/**
*
* @description 功能描述: get 请求
* @return 返回类型:
*/
public static String get(String url) {
return get(url, null);
}

/**
*
* @description 功能描述: get 请求
* @return 返回类型:
* @throws UnsupportedEncodingException
*/
public static String get(String url, Map<String, String> params) {
return get(url, params, null);
}

/**
*
* @description 功能描述: POST 请求
* @return 返回类型:
*/
public static String post(String url, String params, Map<String, String> headers) {
StringBuffer bufferRes = null;
try {
HttpURLConnection http = null;
if (isHttps(url)) {
http = initHttps(url, _POST, headers);
} else {
http = initHttp(url, _POST, headers);
}
OutputStream out = http.getOutputStream();
out.write(params.getBytes(DEFAULT_CHARSET));
out.flush();
out.close();
String tgt = http.getHeaderField("location");
InputStream in = http.getInputStream();
BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));
String valueString = null;
bufferRes = new StringBuffer();
while ((valueString = read.readLine()) != null) {
bufferRes.append(valueString);
}
read.close();
in.close();
if (http != null) {
http.disconnect();// 关闭连接
}
if(StrUtils.isEmpty(tgt))
{
return bufferRes.toString();
}else
{
return tgt;
}
//
//
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

private static String prepareParam(Map<String,Object> paramMap){
StringBuffer sb = new StringBuffer();
if(null==paramMap||paramMap.isEmpty()){
return "" ;
}else{
for(String key: paramMap.keySet()){
String value = (String)paramMap.get(key);
if(sb.length()<1){
sb.append(key).append("=").append(value);
}else{
sb.append("&").append(key).append("=").append(value);
}
}
return sb.toString();
}
}
/**
*
* @description 功能描述: POST 请求
* @return 返回类型:
*/
public static void doDelete(String urlStr,Map<String,Object> paramMap) throws Exception{
String paramStr = prepareParam(paramMap);
if(paramStr == null || paramStr.trim().length()<1){

}else{
urlStr +="?"+paramStr;
}
System.out.println(urlStr);
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod(_DELETE);
//屏蔽掉的代码是错误的,java.net.ProtocolException: HTTP method DELETE doesn‘t support output
/* OutputStream os = conn.getOutputStream();
os.write(paramStr.toString().getBytes("utf-8"));
os.close(); */

if(conn.getResponseCode() ==200){
System.out.println("成功");
}else{
System.out.println(conn.getResponseCode());
}
}

/**
* post map 请求
*
* @param url
* @param params
* @return
* @throws UnsupportedEncodingException
*/
public static String post(String url, Map<String, String> params) throws UnsupportedEncodingException {
return post(url, map2Url(params), null);
}

/**
* post map 请求,headers请求头
*
* @param url
* @param params
* @return
* @throws UnsupportedEncodingException
*/
public static String post(String url, Map<String, String> params, Map<String, String> headers)
throws UnsupportedEncodingException {
return post(url, map2Url(params), headers);
}

/**
*
* @description 功能描述: 构造请求参数
* @return 返回类型:
* @throws UnsupportedEncodingException
*/
public static String initParams(String url, Map<String, String> params) throws UnsupportedEncodingException {
if (null == params || params.isEmpty()) {
return url;
}
StringBuilder sb = new StringBuilder(url);
if (url.indexOf("?") == -1) {
sb.append("?");
}
sb.append(map2Url(params));
return sb.toString();
}

/**
* map构造url
*
* @description 功能描述:
* @return 返回类型:
* @throws UnsupportedEncodingException
*/
public static String map2Url(Map<String, String> paramToMap) throws UnsupportedEncodingException {
if (null == paramToMap || paramToMap.isEmpty()) {
return null;
}
StringBuffer url = new StringBuffer();
boolean isfist = true;
for (Entry<String, String> entry : paramToMap.entrySet()) {
if (isfist) {
isfist = false;
} else {
url.append("&");
}
url.append(entry.getKey()).append("=");
String value = entry.getValue();
if (StrUtils.isNotEmpty(value)) {
url.append(URLEncoder.encode(value, DEFAULT_CHARSET));
}
}
return url.toString();
}

/**
* 检测是否https
*
* @param url
*/
private static boolean isHttps(String url) {
return url.startsWith("https");
}

/**
* https 域名校验
*
* @param url
* @param params
* @return
*/
public class TrustAnyHostnameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;// 直接返回true
}
}
}

// 证书管理
class MyX509TrustManager implements X509TrustManager {

public X509Certificate[] getAcceptedIssuers() {
return null;
}

public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}

public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}

}

package com.wolf.eureka.client;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

public class Test {

public static void main(String[] args) throws UnsupportedEncodingException {
String url = "http://localhost:8080/cas/v1/tickets";
Map<String, String> params = new HashMap<String, String>();
params.put("username", "1");
params.put("password", "1");
params.put("service", "http://www.google.com");
//获取TGT
String resoult = HttpKit.post(url, params);
System.out.println(resoult);
String tgt= resoult.substring(resoult.lastIndexOf("/") + 1);
System.out.println("Tgt is : " + tgt);
String urlST = url+"/"+tgt;
params.clear();
params.put("service", "http://www.google.com");
resoult = HttpKit.post(urlST,params);
//获取 ST
System.out.println("st is : " + resoult);
try {
resoult = HttpKit.post(url+"/remove/"+tgt,params);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("remove : " + resoult);

}

}

时间: 2024-12-19 20:25:52

自定义Caslogout的相关文章

Linux centos下让alias自定义永久生效(+常用的别名)

alias可以简化一些复杂的命令串,使一个单词或简化后的命令即可实现复杂(通常是带很多参数的长串)命令. 基本用法: alias 简化命令='实际的长串命令'    //实际长串命令通常为'原命令 -/选项参数' eg. alias ll='ls -a' 获取别名: alias        //即可查看当前设定的所有alias别名 取消别名: unalias 简化命令 eg. unalias ll            //取消ll的别名 永久生效: 直接使用alias命令定义的别名,重启后就

MVC4 自定义错误页面(三)

一.概述 MVC4框架自带了定义错误页,该页面位于Shared/Error,该页面能够显示系统未能捕获的异常,如何才能使用该页面: 二.使用步骤: 1.配置WebConfig文件,在System.Web节点下加上 <customErrors mode="On"  defaultRedirect="~/Shared/Error" /> 翻阅一些大神写的博客,在他们的博客中指出defaultRedirect是指向错误页面的URL,可是经过本人测试的时候,发现

自定义及发布一个webservice服务

自定义及发布一个webservice服务    - 声明 某个业务服务为webservice服务       通过@webservice 注解来声明    - 发布webservice服务       Endpoint.publish()发布 (默认对public修饰的方法进行发布)    - 通过wsimport生成本地代理来访问自己发布的webservice       wsimport 1.发布自定义webservice phone.java package ws.myWebService

前端angularJS利用directive实现移动端自定义软键盘的方法

最近公司项目的需求上要求我们iPad项目上一些需要输入数字的地方用我们自定义的软键盘而不是移动端设备自带的键盘,刚接到需求有点懵,因为之前没有做过,后来理了一下思路发现这东西也就那样.先看一下实现之后的效果: 实现的效果就是当点击页面中需要弹出软键盘的时候软键盘弹出,浮在页面的中间,和模态框一样的效果,可以在软键盘中输入任何数字,附带的功能有小数点.退格.清空.确定等功能.当在键盘上点击数字的时候页面中的表单中实时的添加对应的数字,上图中可以看到. 产品经理那边给的原因是iPad屏幕本来就小,如

自定义input[type=&quot;checkbox&quot;]的样式

对复选框自定义样式,我们以前一直用的脚本来实现,不过现在可以使用新的伪类 :checkbox 来实现. 如果直接对复选框设置样式,那么这个伪类并不实用,因为没有多少样式能够对复选框起作用.不过,倒是可以基于复选框的勾选状态借助组合选择符来给其他元素设置样式. 很多时候,无论是为了表单元素统一,还是为了用户体验良好,我们都会选择 label 元素和 input[type="checkbox"] 一起使用.当<label>元素与复选框关联之后,也可以起到触发开关的作用. 思路:

[CSS揭秘]自定义单选框和复选框

很多Web前端工程师肯定都会遇到过,在项目中,UI设计师跑来跟你说,我这个地方的表单控件需要设计成这个样子那个样子.但是用CSS却不能够修改这些控件的默认样式,于是乎,只能通过div+javascript技术来进行模拟.特别是在如今移动端的崛起时代,更加注重用户的体验.于是就更加需要这样一种hack技术. 如果对如今的前端框架有过了解,都会知道组件这个概念.那么在这些框架中,都会提供一些单选框或复选框按钮组件.可见大家之前受到表单元素的默认样式的毒害有多深. 今天先给大家简单介绍一下如何通过CS

自定义元素(custom elements)

记录下自定义html自定义元素的相关心得: 浏览器将自定义元素保留在 DOM 之中,但不会任何语义.除此之外,自定义元素与标准元素都一致 事实上,浏览器提供了一个HTMLUnknownElement,HTMLElement对象,所有自定义元素都是该对象的实例. var tabs=document.createElement("tabs"); console.log(tabs instanceof HTMLUnknownElement);//true console.log(tabs i

wex5 教程 自定义action与名称去重

有一个订单,需要取出不同的客户名称,且只显示一次名称.效果如下图: 一 思路:自定义action,用sql语句的group by分组,将名称返回前端,用list显示出来. 二 制作步骤: 1 新建service service作为后端服务分发接口,一个工程可以有多个service,一个service可以有多个action. 2  新建action action基本参数: 名称:自定义 实现:(指向 java方法) 格式为 Name.getName   其中Name为java类,首写字母必须为大写

springmvc 类型转换器 自定义类型转换器

自定义类型转换器的步骤: 1.定义类型转换器 2.类型转换器的注册(在springmvc配置文件处理) 来解决多种日期格式的问题: