13693261870
2022-09-20 a666b5f9741ef9b21f547d3b2141752a0383c70c
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
package org.apereo.cas.web.flow;
 
import com.zaxxer.hikari.HikariDataSource;
 
import java.io.PrintStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apereo.cas.authentication.MessageDescriptor;
import org.apereo.cas.configuration.CasConfigurationProperties;
import org.apereo.cas.web.landtool.Checkpass;
import org.apereo.cas.web.landtool.terra.OjdbcProperties;
import org.apereo.cas.web.landtool.utils.Md5Util;
import org.apereo.cas.web.support.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.binding.message.MessageBuilder;
import org.springframework.binding.message.MessageContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.webflow.action.AbstractAction;
import org.springframework.webflow.core.collection.MutableAttributeMap;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.RequestContext;
 
/**
 * @author Tanbin
 * @date   2018-12-12
 */
@Component("CheckAndUpdateAction")
public class CheckAndUpdateAction
        extends AbstractAction {
    @Autowired
    private CasConfigurationProperties casProperties;
    @Autowired
    private OjdbcProperties properties;
 
    private static final String mService="service";
 
    protected static void addMessageDescriptorToMessageContext(MessageContext context, MessageDescriptor warning) {
        MessageBuilder builder = new MessageBuilder()
                .warning()
                .code(warning.getCode())
                .defaultText(warning.getDefaultMessage())
                .args(warning.getParams());
        context.addMessage(builder.build());
    }
 
    @Override
    protected Event doExecute(RequestContext requestContext)
            throws Exception {
        Checkpass cps = (Checkpass) requestContext.getFlowScope().get("checkpass");
        HttpServletRequest hsr = WebUtils.getHttpServletRequest();
 
        String userpassword = cps.getPassword();
        String confirmuserpassword = cps.getConfirmedPassword();
        String regex = "^.*(?=.{8,})(?=.*\\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*?.]).*$";
        if (!userpassword.matches(regex)) {
            return new Event(this, "formaterror");
        }
        String username = requestContext.getFlowScope().get("credential").toString();
        if (userpassword.equals(confirmuserpassword)) {
            String finalpass = Md5Util.reverse(Md5Util.generate(userpassword));
            String updatesql = String.format(this.properties.getUpdatesql(), new Object[]{finalpass, username});
            int result = getJdbcTemplate().update(updatesql);
            if (result > 0) {
                if (!StringUtils.isEmpty(hsr.getParameter(mService))) {
                    WebUtils.getHttpServletResponse().sendRedirect(hsr.getParameter("service"));
                } else {
                    return new Event(this, "error");
                }
 
            } else {
                return new Event(this, "authenticationFailure");
            }
        }
        return null;
    }
 
 
    protected JdbcTemplate getJdbcTemplate() {
        HikariDataSource da = new HikariDataSource();
        da.setJdbcUrl(this.properties.getJdbcUrl());
        da.setAutoCommit(true);
        da.setDriverClassName(this.properties.getDriverclass());
        da.setUsername(this.properties.getUsername());
        da.setPassword(this.properties.getPassword());
        JdbcTemplate jdbc = new JdbcTemplate(da);
 
        return jdbc;
    }
 
}