Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mksgroup.myworkspace.dailyreport;
- import javax.servlet.http.HttpSessionEvent;
- import org.jasig.cas.client.authentication.AttributePrincipal;
- import org.jasig.cas.client.session.SingleSignOutFilter;
- import org.jasig.cas.client.session.SingleSignOutHttpSessionListener;
- import org.jasig.cas.client.validation.Cas30ServiceTicketValidator;
- import org.jasig.cas.client.validation.TicketValidator;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.boot.builder.SpringApplicationBuilder;
- import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Primary;
- import org.springframework.context.annotation.PropertySource;
- import org.springframework.context.event.EventListener;
- import org.springframework.security.cas.ServiceProperties;
- import org.springframework.security.cas.authentication.CasAssertionAuthenticationToken;
- import org.springframework.security.cas.authentication.CasAuthenticationProvider;
- import org.springframework.security.cas.web.CasAuthenticationEntryPoint;
- import org.springframework.security.core.authority.AuthorityUtils;
- import org.springframework.security.core.userdetails.AuthenticationUserDetailsService;
- import org.springframework.security.core.userdetails.User;
- import org.springframework.security.web.AuthenticationEntryPoint;
- import org.springframework.security.web.authentication.logout.LogoutFilter;
- import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
- @SpringBootApplication
- @PropertySource("classpath:application.properties")
- public class MyWorkspaceApplication extends SpringBootServletInitializer {
- @Value("${cas.service}")
- private String service;
- @Value("${cas.loginUrl}")
- private String loginUrl;
- @Value("${cas.cas30ServiceTicketValidator}")
- private String cas30ServiceTicketValidator;
- @Value("${cas.createAuthorityList}")
- private String createAuthorityList;
- @Value("${cas.key}")
- private String key;
- @Value("${cas.logoutFilterLink}")
- private String logoutFilterLink;
- @Value("${cas.setFilterProcessesUrl}")
- private String setFilterProcessesUrl;
- @Value("${cas.setCasServerUrlPrefix}")
- private String setCasServerUrlPrefix;
- @Override
- protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
- return application.sources(MyWorkspaceApplication.class);
- }
- public static void main(String[] args) {
- SpringApplication.run(MyWorkspaceApplication.class, args);
- }
- @Bean
- public ServiceProperties serviceProperties() {
- ServiceProperties serviceProperties = new ServiceProperties();
- serviceProperties.setService(service);
- serviceProperties.setSendRenew(false);
- return serviceProperties;
- }
- @Bean
- @Primary
- public AuthenticationEntryPoint authenticationEntryPoint(ServiceProperties sP) {
- CasAuthenticationEntryPoint entryPoint = new CasAuthenticationEntryPoint();
- entryPoint.setLoginUrl(loginUrl);
- entryPoint.setServiceProperties(sP);
- return entryPoint;
- }
- @Bean
- public TicketValidator ticketValidator() {
- return new Cas30ServiceTicketValidator(cas30ServiceTicketValidator);
- }
- @Bean
- public CasAuthenticationProvider casAuthenticationProvider() {
- CasAuthenticationProvider provider = new CasAuthenticationProvider();
- provider.setServiceProperties(serviceProperties());
- provider.setTicketValidator(ticketValidator());
- // provider.setUserDetailsService((s) -> new User("admin", "password",
- // true, true, true, true,
- // AuthorityUtils.createAuthorityList(createAuthorityList)));
- provider.setAuthenticationUserDetailsService(customUserDetailsService());
- provider.setKey(key);
- return provider;
- }
- //cua MyCAS
- @Bean
- public AuthenticationUserDetailsService<CasAssertionAuthenticationToken> customUserDetailsService() {
- return token -> {
- AttributePrincipal principal = token.getAssertion().getPrincipal();
- String name = principal.getName();
- System.out.println(name+ "-----------------------------");
- Object roles = principal.getAttributes().get("ROLES");
- System.out.println(roles+ "-----------------------------");
- // List<GrantedAuthority> authorities = new ArrayList<>();
- // if (roles instanceof List) {
- // List<String> list = (List<String>) roles;
- // list.forEach(role -> {
- // GrantedAuthority authority = new SimpleGrantedAuthority(role);
- // authorities.add(authority);
- // });
- // }
- // System.out.println("mywworkspace"+AuthorityUtils.createAuthorityList(createAuthorityList).toString());
- return new User(name, "pwd", AuthorityUtils.createAuthorityList(createAuthorityList));
- };
- }
- // @Bean
- // public CasAuthenticationProvider casAuthenticationProvider() {
- // CasAuthenticationProvider provider = new CasAuthenticationProvider();
- // provider.setServiceProperties(serviceProperties());
- // provider.setTicketValidator(ticketValidator());
- // // provider.setUserDetailsService((s) -> new User("hoaiem","hoaiem",
- // // true, true, true, true,
- // // AuthorityUtils.createAuthorityList("ROLE_USER","ROLE_LEADER")));
- // UserDetailsService user = new UserDetailsService() {
- //
- // @Override
- // public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
- // if(!"hoaiem".equalsIgnoreCase(userName)) throw new UsernameNotFoundException("User name not found");
- // String password = "123";
- // List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
- // SimpleGrantedAuthority authority = new SimpleGrantedAuthority("ROLE_USER");
- // SimpleGrantedAuthority authority2 = new SimpleGrantedAuthority("ROLE_LEADER");
- // authorities.add(authority);
- // authorities.add(authority2);
- // MyUserDetails userDetail = new MyUserDetails(userName, password, authorities);
- // return userDetail;
- // }
- // };
- // provider.setUserDetailsService(user);
- // provider.setKey("CAS_PROVIDER_LOCALHOST_8000");
- // return provider;
- // }
- @Bean
- public SecurityContextLogoutHandler securityContextLogoutHandler() {
- return new SecurityContextLogoutHandler();
- }
- @Bean
- public LogoutFilter logoutFilter() {
- LogoutFilter logoutFilter = new LogoutFilter(logoutFilterLink, securityContextLogoutHandler());
- logoutFilter.setFilterProcessesUrl(setFilterProcessesUrl);
- return logoutFilter;
- }
- @Bean
- public SingleSignOutFilter singleSignOutFilter() {
- SingleSignOutFilter singleSignOutFilter = new SingleSignOutFilter();
- singleSignOutFilter.setCasServerUrlPrefix(setCasServerUrlPrefix);
- singleSignOutFilter.setIgnoreInitConfiguration(true);
- return singleSignOutFilter;
- }
- @EventListener
- public SingleSignOutHttpSessionListener singleSignOutHttpSessionListener(HttpSessionEvent event) {
- return new SingleSignOutHttpSessionListener();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement