1.在pom.xml中添加:

&lt;<span><strong>dependency</strong></span>&gt;
    &lt;<span><strong>groupId</strong></span>&gt;org.pac4j&lt;/<span><strong>groupId</strong></span>&gt;
    &lt;<span><strong>artifactId</strong></span>&gt;pac4j-cas&lt;/<span><strong>artifactId</strong></span>&gt;
    &lt;<span><strong>version</strong></span>&gt;2.1.0&lt;/<span><strong>version</strong></span>&gt;
&lt;/<span><strong>dependency</strong></span>&gt;
&lt;<span><strong>dependency</strong></span>&gt;
    &lt;<span><strong>groupId</strong></span>&gt;io.buji&lt;/<span><strong>groupId</strong></span>&gt;
    &lt;<span><strong>artifactId</strong></span>&gt;buji-pac4j&lt;/<span><strong>artifactId</strong></span>&gt;
    &lt;<span><strong>version</strong></span>&gt;3.0.0&lt;/<span><strong>version</strong></span>&gt;
&lt;/<span><strong>dependency</strong></span>&gt;
<span>2.新增Pac4jConfig.java文件</span>
<pre data-index="5" name="code"></pre><br>

3.修改ShiroConfig.java文件

<span>    增加 </span>
<span></span><pre data-index="9" name="code"></pre><br>
<span>修改Bean  shiroFilter 方法,增加</span>
1
2
3
4
5
6
7
CallbackFilter callbackFilter = new CallbackFilter();

callbackFilter.setConfig(config);

callbackFilter.setDefaultUrl("/starter");

shiroFilterFactoryBean.getFilters().put("casFilter", callbackFilter);

//拦截器中增加callback的拦截

<span>Map</span>&lt;<span><strong>String</strong></span>, <span><strong>String</strong></span>&gt; filterChainDefinitionMap = <span><strong>new </strong></span><span>LinkedHashMap</span>&lt;<span><strong>String</strong></span>, <span><strong>String</strong></span>&gt;();
filterChainDefinitionMap.<span>put</span>(<span>"/callback"</span>, <span>"casFilter"</span>);
shiroFilterFactoryBean.<span>setFilterChainDefinitionMap</span>(filterChainDefinitionMap);

//loginUrl中需要加上clinetname

<span><strong>String </strong></span>loginUrl = <span>casServerUrlPrefix </span>+ <span>"/login?service=" </span>+ <span>shiroServerUrlPrefix </span>+ <span>"/callback?client_name=" </span>+ <span>clientName</span>;
shiroFilterFactoryBean.<span>setLoginUrl</span>(loginUrl);
<span>4.自定义的Realm不再继承自CasRealm,修改为Pac4jRealm,并且修改其中的方法</span>
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
@Override

protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

final Pac4jToken token = (Pac4jToken) authenticationToken;

final LinkedHashMap<String, CommonProfile> profiles = token.getProfiles();

final Pac4jPrincipal principal = new Pac4jPrincipal(profiles);

String loginName = principal.getProfile().getId();

Session session = SecurityUtils.getSubject().getSession();

session.setAttribute("userSessionId", loginName );

return new SimpleAuthenticationInfo(user, profiles.hashCode(), getName());

}

//此方法的逻辑不变,还是在此处赋请求链接权限,只是改变获取用户的方法更改一下,可以将本地查询的用户信息保存在doGetAuthenticationInfo方法中,此方法可以直接取出来

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
@Override

protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

Session session = SecurityUtils.getSubject().getSession();

String loginName = (String)session.getAttribute("name");

return info;

}