1
13693261870
2022-09-16 58d012f11dd34564d81b4eb3a6099eb689876597
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package org.apereo.cas.web.flow;
 
import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
 
import org.apereo.cas.CentralAuthenticationService;
import org.apereo.cas.authentication.Authentication;
import org.apereo.cas.authentication.AuthenticationBuilder;
import org.apereo.cas.authentication.AuthenticationResult;
import org.apereo.cas.authentication.AuthenticationResultBuilder;
import org.apereo.cas.authentication.BasicCredentialMetaData;
import org.apereo.cas.authentication.CredentialMetaData;
import org.apereo.cas.authentication.DefaultAuthenticationBuilder;
import org.apereo.cas.authentication.DefaultAuthenticationResult;
import org.apereo.cas.authentication.DefaultAuthenticationResultBuilder;
import org.apereo.cas.authentication.DefaultHandlerResult;
import org.apereo.cas.authentication.HandlerResult;
import org.apereo.cas.authentication.UsernamePasswordCredential;
import org.apereo.cas.authentication.principal.DefaultPrincipalFactory;
import org.apereo.cas.authentication.principal.Principal;
import org.apereo.cas.services.ServicesManager;
import org.apereo.cas.support.rest.DefaultCredentialFactory;
import org.apereo.cas.ticket.TicketGrantingTicket;
import org.apereo.cas.web.support.WebUtils;
import org.apereo.inspektr.common.web.ClientInfoHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.webflow.action.AbstractAction;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.RequestContext;
 
import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.UserAgent;
import eu.bitwalker.useragentutils.Version;
 
@Component("loginAction2")
public class LoginAction2 extends AbstractAction {
      protected Logger logger = LoggerFactory.getLogger(getClass());
      
        /**
         * The Central authentication service.
         */
      @Autowired
      protected CentralAuthenticationService centralAuthenticationService;
    
      protected ServicesManager servicesManager;
      @Override
      protected Event doExecute(RequestContext requestContext) throws Exception {
          final TicketGrantingTicket tgt;
          final Map<String, HandlerResult> successes=new HashMap<>();
        String username=  requestContext.getFlowScope().get("username").toString();
          UsernamePasswordCredential credential=new UsernamePasswordCredential(username,"");
          CredentialMetaData cmd= new BasicCredentialMetaData(credential);
          DefaultPrincipalFactory p=new DefaultPrincipalFactory();
          Principal principal=p.createPrincipal(username);
          HandlerResult hs= new DefaultHandlerResult("handler",cmd,principal,null);
           successes.put("success",   hs);
          final Map<String, Object> authenticationAttributes = new HashMap<>();
          AuthenticationBuilder d=DefaultAuthenticationBuilder.newInstance();
        
          HttpServletRequest request = WebUtils.getHttpServletRequest();
            if(null != request) {
                String uaStr = request.getHeader("User-Agent");
                UserAgent ua = UserAgent.parseUserAgentString(uaStr);
                if(null != ua) {
                    Browser browser=ua.getBrowser();
                    Version broVersion=browser.getVersion(uaStr);
                    final String browserInfo=browser.getName()+"/"+broVersion.getVersion();
                    final String clientIp = ClientInfoHolder.getClientInfo().getClientIpAddress();
                    String serviceUrl = request.getParameter("service");
                    logger.debug("##### client IP address is [{}], browser is [{}], serviceUrl is [{}]", clientIp, browserInfo, serviceUrl);
                    authenticationAttributes.put("clientIp", clientIp);
                    authenticationAttributes.put("browserInfo", browserInfo);
                    authenticationAttributes.put("serviceUrl", serviceUrl);
                }
            }
          //接收传来的用户名
          d.setPrincipal(principal);
            d.addCredential(cmd);
           d.setAttributes(authenticationAttributes);
          d.setAuthenticationDate(ZonedDateTime.now());
          d.setSuccesses(successes);
          Authentication authentication= d.build();
         
           final DefaultAuthenticationResult res = new DefaultAuthenticationResult(authentication, null);
       
          tgt = this.centralAuthenticationService.createTicketGrantingTicket(res);;
           WebUtils.putTicketGrantingTicketInScopes(requestContext, tgt);
            WebUtils.putAuthenticationResult(res, requestContext);
            WebUtils.putAuthentication(tgt.getAuthentication(), requestContext);
    //    return new Event(this,"directsubmit");
        return new Event(this,"success");
      }
      
      public void setServicesManager(ServicesManager servicesManager) { this.servicesManager = servicesManager; }
}