在使用Ajax访问CAS保护的网址时会报一下的错误:
(1)XMLHttpRequest cannot load https://zhangsan-pc:8443/cas/login?service=http%3A%2F%2Fzhanshan-PC%3A8090%2Fgtportal%2FCASUserInfoServlet. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8090' is therefore not allowed access.
(2)NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://localhost:8090/gtportal/CASUserInfoServlet?callback=feedBackState&_=1422262265310'."
解决方法:
(1)修改cas客户端的过滤方法,如果你访问的服务不是怎么重要的信息时就可以使用此方法,修改client的org.jasig.cas.client.authentication包下面的AuthenticationFilter,在该类里面doFilter方法中判断一下如果该路径是你不需要过滤的地址就直接返回,该地址可以在web.xml中进行配置。AuthenticationFilter的修改详细见附件。
(2)还有一个问题是Ajax的跨域问题引起的,在这种情况下,可以使用js的<script>标签来代替Ajax.可以动态的向页面添加<script>.代码如下:
/**通过script标签向服务端发出请求 * */
function getUserInfo()
{
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "../../CASUserInfoServlet?callback=initMenu";
document.getElementById('ipotalHeader_new').appendChild(script);
},