##### Coverage Report - com.ctrip.framework.apollo.portal.configutation.AuthConfiguration

Classes in this File | Line Coverage | Branch Coverage | Complexity
AuthConfiguration |
100% | 2/2
N/A | N/A
| 1.5;1.5
AuthConfiguration$CtripAuthAutoConfiguration |
0% | 0/49
N/A | N/A
| 1.5;1.5
AuthConfiguration$DefaultAuthAutoConfiguration |
100% | 3/3
N/A | N/A
| 1.5;1.5

1 |  |
     package com.ctrip.framework.apollo.portal.configutation;
2 |  |
3 |  |
     import com.ctrip.framework.apollo.portal.auth.CtripLogoutHandler;
4 |  |
     import com.ctrip.framework.apollo.portal.auth.CtripUserInfoHolder;
5 |  |
     import com.ctrip.framework.apollo.portal.auth.DefaultLogoutHandler;
6 |  |
     import com.ctrip.framework.apollo.portal.auth.DefaultUserInfoHolder;
7 |  |
     import com.ctrip.framework.apollo.portal.auth.LogoutHandler;
8 |  |
     import com.ctrip.framework.apollo.portal.auth.UserInfoHolder;
9 |  |
     import com.ctrip.framework.apollo.portal.repository.ServerConfigRepository;
10 |  |
11 |  |
     import org.springframework.beans.factory.annotation.Autowired;
12 |  |
     import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
13 |  |
     import org.springframework.boot.context.embedded.FilterRegistrationBean;
14 |  |
     import org.springframework.boot.context.embedded.ServletListenerRegistrationBean;
15 |  |
     import org.springframework.context.annotation.Bean;
16 |  |
     import org.springframework.context.annotation.Configuration;
17 |  |
     import org.springframework.context.annotation.Profile;
18 |  |
19 |  |
     import java.util.EventListener;
20 |  |
     import java.util.HashMap;
21 |  |
     import java.util.Map;
22 |  |
23 |  |
     import javax.servlet.Filter;
24 |  |
25 |  |
26 |  |
     /**
27 |  |
      * sso相关的配置.
28 |  |
      */
29 |  |
     @Configuration
30 | 1 |
     public class AuthConfiguration {
31 |  |
32 |  |
33 |  |
       /**
34 |  |
        * 在ctrip内部运行时,会指定 spring.profiles.active = ctrip.
35 |  |
        * ctrip sso是通过cas实现的,所以需要加载相关的filter和listener.
36 |  |
        */
37 |  |
       @Configuration
38 |  |
       @Profile("ctrip")
39 | 0 |
       static class CtripAuthAutoConfiguration {
40 |  |
41 |  |
         @Autowired
42 |  |
         private ServerConfigRepository serverConfigRepository;
43 |  |
44 |  |
         @Bean
45 |  |
         public ServletListenerRegistrationBean redisAppSettingListner(){
46 | 0 |
           ServletListenerRegistrationBean redisAppSettingListner = new ServletListenerRegistrationBean();
47 | 0 |
           redisAppSettingListner.setListener(listener("org.jasig.cas.client.credis.CRedisAppSettingListner"));
48 | 0 |
           return redisAppSettingListner;
49 |  |
         }
50 |  |
51 |  |
         @Bean
52 |  |
         public ServletListenerRegistrationBean singleSignOutHttpSessionListener(){
53 | 0 |
           ServletListenerRegistrationBean singleSignOutHttpSessionListener = new ServletListenerRegistrationBean();
54 | 0 |
           singleSignOutHttpSessionListener.setListener(listener("org.jasig.cas.client.session.SingleSignOutHttpSessionListener"));
55 | 0 |
           return singleSignOutHttpSessionListener;
56 |  |
         }
57 |  |
58 |  |
         @Bean
59 |  |
         public FilterRegistrationBean casFilter(){
60 | 0 |
           FilterRegistrationBean singleSignOutFilter = new FilterRegistrationBean();
61 | 0 |
           singleSignOutFilter.setFilter(filter("org.jasig.cas.client.session.SingleSignOutFilter"));
62 | 0 |
           singleSignOutFilter.addUrlPatterns("/*");
63 | 0 |
           return singleSignOutFilter;
64 |  |
         }
65 |  |
66 |  |
         @Bean
67 |  |
         public FilterRegistrationBean authenticationFilter(){
68 | 0 |
           FilterRegistrationBean casFilter = new FilterRegistrationBean();
69 |  |
70 | 0 |
           Map<String, String> filterInitParam = new HashMap();
71 | 0 |
           filterInitParam.put("redisClusterName", "casClientPrincipal");
72 | 0 |
           filterInitParam.put("serverName", serverConfigRepository.findByKey("serverName").getValue());
73 | 0 |
           filterInitParam.put("casServerLoginUrl", serverConfigRepository.findByKey("casServerLoginUrl").getValue());
74 |  |
75 | 0 |
           casFilter.setInitParameters(filterInitParam);
76 | 0 |
           casFilter.setFilter(filter("org.jasig.cas.client.authentication.AuthenticationFilter"));
77 | 0 |
           casFilter.addUrlPatterns("/*");
78 |  |
79 | 0 |
           return casFilter;
80 |  |
         }
81 |  |
82 |  |
         @Bean
83 |  |
         public FilterRegistrationBean casValidationFilter(){
84 | 0 |
           FilterRegistrationBean casValidationFilter = new FilterRegistrationBean();
85 | 0 |
           Map<String, String> filterInitParam = new HashMap();
86 | 0 |
           filterInitParam.put("casServerUrlPrefix", serverConfigRepository.findByKey("casServerUrlPrefix").getValue());
87 | 0 |
           filterInitParam.put("serverName", serverConfigRepository.findByKey("serverName").getValue());
88 | 0 |
           filterInitParam.put("encoding", "UTF-8");
89 | 0 |
           filterInitParam.put("useRedis", "true");
90 | 0 |
           filterInitParam.put("redisClusterName", "casClientPrincipal");
91 |  |
92 | 0 |
           casValidationFilter.setFilter(filter("org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter"));
93 | 0 |
           casValidationFilter.setInitParameters(filterInitParam);
94 | 0 |
           casValidationFilter.addUrlPatterns("/*");
95 |  |
96 | 0 |
           return casValidationFilter;
97 |  |
98 |  |
         }
99 |  |
100 |  |
101 |  |
102 |  |
         @Bean
103 |  |
         public FilterRegistrationBean assertionHolder(){
104 | 0 |
           FilterRegistrationBean assertionHolderFilter = new FilterRegistrationBean();
105 |  |
106 | 0 |
           assertionHolderFilter.setFilter(filter("org.jasig.cas.client.util.AssertionThreadLocalFilter"));
107 | 0 |
           assertionHolderFilter.addUrlPatterns("/*");
108 |  |
109 | 0 |
           return assertionHolderFilter;
110 |  |
         }
111 |  |
112 |  |
         @Bean
113 |  |
         public CtripUserInfoHolder ctripUserInfoHolder(){
114 | 0 |
           return new CtripUserInfoHolder();
115 |  |
         }
116 |  |
117 |  |
         @Bean
118 |  |
         public CtripLogoutHandler logoutHandler(){
119 | 0 |
           return new CtripLogoutHandler();
120 |  |
         }
121 |  |
122 |  |
         private Filter filter(String className){
123 | 0 |
           Class clazz = null;
124 |  |
           try {
125 | 0 |
             clazz = Class.forName(className);
126 | 0 |
             Object obj = clazz.newInstance();
127 | 0 |
             return (Filter) obj;
128 | 0 |
           } catch (Exception e) {
129 | 0 |
             throw new RuntimeException("instance filter fail", e);
130 |  |
           }
131 |  |
132 |  |
         }
133 |  |
134 |  |
         private EventListener listener(String className){
135 | 0 |
           Class clazz = null;
136 |  |
           try {
137 | 0 |
             clazz = Class.forName(className);
138 | 0 |
             Object obj = clazz.newInstance();
139 | 0 |
             return (EventListener) obj;
140 | 0 |
           } catch (Exception e) {
141 | 0 |
             throw new RuntimeException("instance listener fail", e);
142 |  |
           }
143 |  |
         }
144 |  |
       }
145 |  |
146 |  |
       /**
147 |  |
        * 默认实现
148 |  |
        */
149 | 1 |
       @Configuration
150 | 1 |
       static class DefaultAuthAutoConfiguration {
151 |  |
152 |  |
         @Bean
153 |  |
         @ConditionalOnMissingBean(UserInfoHolder.class)
154 |  |
         public DefaultUserInfoHolder notCtripUserInfoHolder(){
155 | 1 |
           return new DefaultUserInfoHolder();
156 |  |
         }
157 |  |
158 |  |
         @Bean
159 |  |
         @ConditionalOnMissingBean(LogoutHandler.class)
160 |  |
         public DefaultLogoutHandler logoutHandler(){
161 | 1 |
           return new DefaultLogoutHandler();
162 |  |
         }
163 |  |
       }
164 |  |
165 |  |
166 |  |
     }