Advertisement
Guest User

Untitled

a guest
Apr 10th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.65 KB | None | 0 0
  1. package mksgroup.myworkspace.dailyreport;
  2.  
  3. import javax.servlet.http.HttpSessionEvent;
  4.  
  5. import org.jasig.cas.client.authentication.AttributePrincipal;
  6. import org.jasig.cas.client.session.SingleSignOutFilter;
  7. import org.jasig.cas.client.session.SingleSignOutHttpSessionListener;
  8. import org.jasig.cas.client.validation.Cas30ServiceTicketValidator;
  9. import org.jasig.cas.client.validation.TicketValidator;
  10. import org.springframework.beans.factory.annotation.Value;
  11. import org.springframework.boot.SpringApplication;
  12. import org.springframework.boot.autoconfigure.SpringBootApplication;
  13. import org.springframework.boot.builder.SpringApplicationBuilder;
  14. import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
  15. import org.springframework.context.annotation.Bean;
  16. import org.springframework.context.annotation.Primary;
  17. import org.springframework.context.annotation.PropertySource;
  18. import org.springframework.context.event.EventListener;
  19. import org.springframework.security.cas.ServiceProperties;
  20. import org.springframework.security.cas.authentication.CasAssertionAuthenticationToken;
  21. import org.springframework.security.cas.authentication.CasAuthenticationProvider;
  22. import org.springframework.security.cas.web.CasAuthenticationEntryPoint;
  23. import org.springframework.security.core.authority.AuthorityUtils;
  24. import org.springframework.security.core.userdetails.AuthenticationUserDetailsService;
  25. import org.springframework.security.core.userdetails.User;
  26. import org.springframework.security.web.AuthenticationEntryPoint;
  27. import org.springframework.security.web.authentication.logout.LogoutFilter;
  28. import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
  29.  
  30.  
  31.  
  32. @SpringBootApplication
  33. @PropertySource("classpath:application.properties")
  34. public class MyWorkspaceApplication extends SpringBootServletInitializer {
  35.  
  36.  
  37. @Value("${cas.service}")
  38. private String service;
  39.  
  40. @Value("${cas.loginUrl}")
  41. private String loginUrl;
  42.  
  43. @Value("${cas.cas30ServiceTicketValidator}")
  44. private String cas30ServiceTicketValidator;
  45.  
  46. @Value("${cas.createAuthorityList}")
  47. private String createAuthorityList;
  48.  
  49. @Value("${cas.key}")
  50. private String key;
  51.  
  52. @Value("${cas.logoutFilterLink}")
  53. private String logoutFilterLink;
  54.  
  55. @Value("${cas.setFilterProcessesUrl}")
  56. private String setFilterProcessesUrl;
  57.  
  58. @Value("${cas.setCasServerUrlPrefix}")
  59. private String setCasServerUrlPrefix;
  60.  
  61. @Override
  62. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  63. return application.sources(MyWorkspaceApplication.class);
  64. }
  65.  
  66. public static void main(String[] args) {
  67. SpringApplication.run(MyWorkspaceApplication.class, args);
  68. }
  69.  
  70. @Bean
  71. public ServiceProperties serviceProperties() {
  72. ServiceProperties serviceProperties = new ServiceProperties();
  73. serviceProperties.setService(service);
  74. serviceProperties.setSendRenew(false);
  75. return serviceProperties;
  76. }
  77.  
  78. @Bean
  79. @Primary
  80. public AuthenticationEntryPoint authenticationEntryPoint(ServiceProperties sP) {
  81. CasAuthenticationEntryPoint entryPoint = new CasAuthenticationEntryPoint();
  82. entryPoint.setLoginUrl(loginUrl);
  83. entryPoint.setServiceProperties(sP);
  84. return entryPoint;
  85. }
  86.  
  87. @Bean
  88. public TicketValidator ticketValidator() {
  89. return new Cas30ServiceTicketValidator(cas30ServiceTicketValidator);
  90. }
  91.  
  92. @Bean
  93. public CasAuthenticationProvider casAuthenticationProvider() {
  94. CasAuthenticationProvider provider = new CasAuthenticationProvider();
  95. provider.setServiceProperties(serviceProperties());
  96. provider.setTicketValidator(ticketValidator());
  97. // provider.setUserDetailsService((s) -> new User("admin", "password",
  98. // true, true, true, true,
  99. // AuthorityUtils.createAuthorityList(createAuthorityList)));
  100. provider.setAuthenticationUserDetailsService(customUserDetailsService());
  101. provider.setKey(key);
  102. return provider;
  103. }
  104.  
  105. //cua MyCAS
  106. @Bean
  107. public AuthenticationUserDetailsService<CasAssertionAuthenticationToken> customUserDetailsService() {
  108. return token -> {
  109. AttributePrincipal principal = token.getAssertion().getPrincipal();
  110. String name = principal.getName();
  111. System.out.println(name+ "-----------------------------");
  112. Object roles = principal.getAttributes().get("ROLES");
  113. System.out.println(roles+ "-----------------------------");
  114. // List<GrantedAuthority> authorities = new ArrayList<>();
  115. // if (roles instanceof List) {
  116. // List<String> list = (List<String>) roles;
  117. // list.forEach(role -> {
  118. // GrantedAuthority authority = new SimpleGrantedAuthority(role);
  119. // authorities.add(authority);
  120. // });
  121. // }
  122.  
  123. // System.out.println("mywworkspace"+AuthorityUtils.createAuthorityList(createAuthorityList).toString());
  124. return new User(name, "pwd", AuthorityUtils.createAuthorityList(createAuthorityList));
  125. };
  126. }
  127.  
  128. // @Bean
  129. // public CasAuthenticationProvider casAuthenticationProvider() {
  130. // CasAuthenticationProvider provider = new CasAuthenticationProvider();
  131. // provider.setServiceProperties(serviceProperties());
  132. // provider.setTicketValidator(ticketValidator());
  133. // // provider.setUserDetailsService((s) -> new User("hoaiem","hoaiem",
  134. // // true, true, true, true,
  135. // // AuthorityUtils.createAuthorityList("ROLE_USER","ROLE_LEADER")));
  136. // UserDetailsService user = new UserDetailsService() {
  137. //
  138. // @Override
  139. // public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
  140. // if(!"hoaiem".equalsIgnoreCase(userName)) throw new UsernameNotFoundException("User name not found");
  141. // String password = "123";
  142. // List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
  143. // SimpleGrantedAuthority authority = new SimpleGrantedAuthority("ROLE_USER");
  144. // SimpleGrantedAuthority authority2 = new SimpleGrantedAuthority("ROLE_LEADER");
  145. // authorities.add(authority);
  146. // authorities.add(authority2);
  147. // MyUserDetails userDetail = new MyUserDetails(userName, password, authorities);
  148. // return userDetail;
  149. // }
  150. // };
  151. // provider.setUserDetailsService(user);
  152. // provider.setKey("CAS_PROVIDER_LOCALHOST_8000");
  153. // return provider;
  154. // }
  155.  
  156. @Bean
  157. public SecurityContextLogoutHandler securityContextLogoutHandler() {
  158. return new SecurityContextLogoutHandler();
  159. }
  160.  
  161. @Bean
  162. public LogoutFilter logoutFilter() {
  163. LogoutFilter logoutFilter = new LogoutFilter(logoutFilterLink, securityContextLogoutHandler());
  164. logoutFilter.setFilterProcessesUrl(setFilterProcessesUrl);
  165. return logoutFilter;
  166. }
  167.  
  168. @Bean
  169. public SingleSignOutFilter singleSignOutFilter() {
  170. SingleSignOutFilter singleSignOutFilter = new SingleSignOutFilter();
  171. singleSignOutFilter.setCasServerUrlPrefix(setCasServerUrlPrefix);
  172. singleSignOutFilter.setIgnoreInitConfiguration(true);
  173. return singleSignOutFilter;
  174. }
  175.  
  176. @EventListener
  177. public SingleSignOutHttpSessionListener singleSignOutHttpSessionListener(HttpSessionEvent event) {
  178. return new SingleSignOutHttpSessionListener();
  179. }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement