Java Code Examples for org.springframework.jdbc.core.JdbcTemplate#setDataSource()

The following examples show how to use org.springframework.jdbc.core.JdbcTemplate#setDataSource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DataSourceService.java    From mapper-generator-javafx with Apache License 2.0 6 votes vote down vote up
/**
 * 测试数据源
 *
 * @param dataSource 数据源
 * @return true 成功 false 失败
 */
public boolean testConnection(DataSource dataSource) {
    HikariDataSource hikariDataSource = new HikariDataSource();
    hikariDataSource.setUsername(dataSource.getUser());
    hikariDataSource.setPassword(dataSource.getPassword());
    hikariDataSource.setJdbcUrl("jdbc:mysql://" + dataSource.getHost() + ":" + dataSource.getPort() + "/" + dataSource.getDatabase() + "?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false&serverTimezone=CTT");
    hikariDataSource.setDriverClassName(dataSource.getDriveName());
    hikariDataSource.setConnectionTimeout(250);
    JdbcTemplate jdbcTemplate = applicationContext.getBean(JdbcTemplate.class);
    jdbcTemplate.setDataSource(hikariDataSource);

    try {
        jdbcTemplate.query("SELECT 1", (rs, rowNum) -> rs.getString(1));
    } catch (Exception e) {
        return false;
    }
    return true;
}
 
Example 2
Source File: DataSourceRoutingIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Before
public void setup() {
    final String SQL_CLIENT_A = "insert into client (id, name) values (1, 'CLIENT A')";
    final String SQL_CLIENT_B = "insert into client (id, name) values (2, 'CLIENT B')";

    JdbcTemplate jdbcTemplate = new JdbcTemplate();
    jdbcTemplate.setDataSource(routingDatasource);

    ClientDatabaseContextHolder.set(ClientDatabase.CLIENT_A);
    jdbcTemplate.execute(SQL_CLIENT_A);
    ClientDatabaseContextHolder.clear();

    ClientDatabaseContextHolder.set(ClientDatabase.CLIENT_B);
    jdbcTemplate.execute(SQL_CLIENT_B);
    ClientDatabaseContextHolder.clear();
}
 
Example 3
Source File: AbstractBatchJdbcTemplateBenchmark.java    From buffer-slayer with Apache License 2.0 6 votes vote down vote up
@Setup
public void setup() {
  dataSource = new DriverManagerDataSource();
  dataSource.setDriverClassName("com.mysql.jdbc.Driver");
  dataSource.setUrl(propertyOr("jdbcUrl", "jdbc:mysql://127.0.0.1:3306?useSSL=false"));
  dataSource.setUsername(propertyOr("username", "root"));
  dataSource.setPassword(propertyOr("password", "root"));

  JdbcTemplate delegate = new JdbcTemplate(dataSource);
  delegate.setDataSource(dataSource);

  proxy = new SenderProxy(new JdbcTemplateSender(delegate));
  proxy.onMessages(updated -> counter.addAndGet(updated.size()));

  reporter = reporter(proxy);
  batch = new BatchJdbcTemplate(delegate, reporter);
  batch.setDataSource(dataSource);

  unbatch = new JdbcTemplate(dataSource);
  unbatch.setDataSource(dataSource);
  unbatch.update(CREATE_DATABASE);
  unbatch.update(DROP_TABLE);
  unbatch.update(CREATE_TABLE);
}
 
Example 4
Source File: StoredProcedureTests.java    From effectivejava with Apache License 2.0 5 votes vote down vote up
/**
 * Confirm our JdbcTemplate is used
 *
 * @throws Exception
 */
@Test
public void testStoredProcedureConfiguredViaJdbcTemplate() throws Exception {
	given(callableStatement.execute()).willReturn(false);
	given(callableStatement.getUpdateCount()).willReturn(-1);
	given(callableStatement.getObject(2)).willReturn(4);
	given(connection.prepareCall("{call " + StoredProcedureConfiguredViaJdbcTemplate.SQL + "(?, ?)}")
			).willReturn(callableStatement);
	JdbcTemplate t = new JdbcTemplate();
	t.setDataSource(dataSource);
	StoredProcedureConfiguredViaJdbcTemplate sp = new StoredProcedureConfiguredViaJdbcTemplate(t);
	assertEquals(sp.execute(1106), 4);
	verify(callableStatement).setObject(1, 1106, Types.INTEGER);
	verify(callableStatement).registerOutParameter(2, Types.INTEGER);
}
 
Example 5
Source File: StoredProcedureTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Confirm our JdbcTemplate is used
 *
 * @throws Exception
 */
@Test
public void testStoredProcedureConfiguredViaJdbcTemplate() throws Exception {
	given(callableStatement.execute()).willReturn(false);
	given(callableStatement.getUpdateCount()).willReturn(-1);
	given(callableStatement.getObject(2)).willReturn(4);
	given(connection.prepareCall("{call " + StoredProcedureConfiguredViaJdbcTemplate.SQL + "(?, ?)}")
			).willReturn(callableStatement);
	JdbcTemplate t = new JdbcTemplate();
	t.setDataSource(dataSource);
	StoredProcedureConfiguredViaJdbcTemplate sp = new StoredProcedureConfiguredViaJdbcTemplate(t);
	assertEquals(4, sp.execute(1106));
	verify(callableStatement).setObject(1, 1106, Types.INTEGER);
	verify(callableStatement).registerOutParameter(2, Types.INTEGER);
}
 
Example 6
Source File: DbConfig.java    From graphouse with Apache License 2.0 5 votes vote down vote up
@Bean
public JdbcTemplate clickHouseJdbcTemplate(
    DataSource clickHouseDataSource,
    @Value("${graphouse.clickhouse.query-timeout-seconds}") int queryTimeoutSeconds
) {
    final JdbcTemplate jdbcTemplate = new JdbcTemplate();
    jdbcTemplate.setDataSource(clickHouseDataSource);
    jdbcTemplate.setQueryTimeout(queryTimeoutSeconds);
    return jdbcTemplate;
}
 
Example 7
Source File: DbConfig.java    From graphouse with Apache License 2.0 5 votes vote down vote up
@Bean
public JdbcTemplate clickHouseJdbcTemplateAutohide(
    DataSource clickHouseDataSource,
    @Value("${graphouse.autohide.clickhouse.query-timeout-seconds}") int autoHideQueryTimeoutSeconds) {
    final JdbcTemplate jdbcTemplate = new JdbcTemplate();
    jdbcTemplate.setDataSource(clickHouseDataSource);
    jdbcTemplate.setQueryTimeout(autoHideQueryTimeoutSeconds);
    return jdbcTemplate;
}
 
Example 8
Source File: CustomerHandlerAuthentication.java    From CAS with Apache License 2.0 4 votes vote down vote up
@Override
protected AuthenticationHandlerExecutionResult doAuthentication(Credential credential) throws GeneralSecurityException, PreventedException {

    CustomCredential customCredential = (CustomCredential) credential;

    String username = customCredential.getUsername();
    String password = customCredential.getPassword();
    String email = customCredential.getEmail();
    String telephone = customCredential.getTelephone();
    String capcha = customCredential.getCapcha();

    ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    String right = attributes.getRequest().getSession().getAttribute("captcha_code").toString();


    if (!capcha.equalsIgnoreCase(right)) {
        throw new CheckCodeErrorException();
    }

    // 添加邮箱和电话的判断逻辑


    System.out.println("username : " + username);
    System.out.println("password : " + password);
    System.out.println("email : " + email);
    System.out.println("telephone : " + telephone);
    System.out.println("capcha : " + capcha);
    System.out.println("right : " + right);


    // JDBC模板依赖于连接池来获得数据的连接,所以必须先要构造连接池
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/cas");
    dataSource.setUsername("root");
    dataSource.setPassword("123");

    // 创建JDBC模板
    JdbcTemplate jdbcTemplate = new JdbcTemplate();
    jdbcTemplate.setDataSource(dataSource);

    String sql = "SELECT * FROM user WHERE username = ?";

    User info = (User) jdbcTemplate.queryForObject(sql, new Object[]{username}, new BeanPropertyRowMapper(User.class));

    System.out.println("database username : " + info.getUsername());
    System.out.println("database password : " + info.getPassword());


    if (info == null) {
        throw new AccountException("Sorry, username not found!");
    }

    if (!info.getPassword().equals(password)) {
        throw new FailedLoginException("Sorry, password not correct!");
    } else {

        final List<MessageDescriptor> list = new ArrayList<>();
        // 可自定义返回给客户端的多个属性信息
        HashMap<String, Object> returnInfo = new HashMap<>();
        returnInfo.put("expired", info.getDisabled());
        returnInfo.put("email", info.getEmail());
        returnInfo.put("username", info.getUsername());
        returnInfo.put("password", info.getPassword());
        returnInfo.put("disabled", info.getDisabled());

        return createHandlerResult(customCredential,
                this.principalFactory.createPrincipal(username, returnInfo), list);
    }


}
 
Example 9
Source File: CustomerHandlerAuthentication.java    From CAS with Apache License 2.0 4 votes vote down vote up
@Override
protected AuthenticationHandlerExecutionResult doAuthentication(Credential credential) throws GeneralSecurityException, PreventedException {

    CustomCredential customCredential = (CustomCredential) credential;

    String username = customCredential.getUsername();
    String password = customCredential.getPassword();
    String email = customCredential.getEmail();
    String telephone = customCredential.getTelephone();
    String capcha = customCredential.getCapcha();

    ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    String right = attributes.getRequest().getSession().getAttribute("captcha_code").toString();


    if(!capcha.equalsIgnoreCase(right)){
        throw new CheckCodeErrorException();
    }

    // 添加邮箱和电话的判断逻辑


    System.out.println("username : " + username);
    System.out.println("password : " + password);
    System.out.println("email : " + email);
    System.out.println("telephone : " + telephone);
    System.out.println("capcha : " + capcha);
    System.out.println("right : " + right);


    // JDBC模板依赖于连接池来获得数据的连接,所以必须先要构造连接池
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/cas");
    dataSource.setUsername("root");
    dataSource.setPassword("123");

    // 创建JDBC模板
    JdbcTemplate jdbcTemplate = new JdbcTemplate();
    jdbcTemplate.setDataSource(dataSource);

    String sql = "SELECT * FROM user WHERE username = ?";

    User info = (User) jdbcTemplate.queryForObject(sql, new Object[]{username}, new BeanPropertyRowMapper(User.class));

    System.out.println("database username : "+ info.getUsername());
    System.out.println("database password : "+ info.getPassword());


    if (info == null) {
        throw new AccountException("Sorry, username not found!");
    }

    if (!info.getPassword().equals(password)) {
        throw new FailedLoginException("Sorry, password not correct!");
    } else {

        final List<MessageDescriptor> list = new ArrayList<>();

        return createHandlerResult(customCredential,
                this.principalFactory.createPrincipal(username, Collections.emptyMap()), list);
    }


}
 
Example 10
Source File: IdServiceFactoryBean.java    From vesta-id-generator with Apache License 2.0 4 votes vote down vote up
private IdService constructDbIdService(String dbUrl, String dbName,
                                       String dbUser, String dbPassword) {
    log.info(
            "Construct Db IdService dbUrl {} dbName {} dbUser {} dbPassword {}",
            dbUrl, dbName, dbUser, dbPassword);

    ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();

    String jdbcDriver = "com.mysql.jdbc.Driver";
    try {
        comboPooledDataSource.setDriverClass(jdbcDriver);
    } catch (PropertyVetoException e) {
        log.error("Wrong JDBC driver {}", jdbcDriver);
        log.error("Wrong JDBC driver error: ", e);
        throw new IllegalStateException("Wrong JDBC driver ", e);
    }

    comboPooledDataSource.setMinPoolSize(5);
    comboPooledDataSource.setMaxPoolSize(30);
    comboPooledDataSource.setIdleConnectionTestPeriod(20);
    comboPooledDataSource.setMaxIdleTime(25);
    comboPooledDataSource.setBreakAfterAcquireFailure(false);
    comboPooledDataSource.setCheckoutTimeout(3000);
    comboPooledDataSource.setAcquireRetryAttempts(50);
    comboPooledDataSource.setAcquireRetryDelay(1000);

    String url = String
            .format("jdbc:mysql://%s/%s?useUnicode=true&amp;characterEncoding=UTF-8&amp;autoReconnect=true",
                    dbUrl, dbName);

    comboPooledDataSource.setJdbcUrl(url);
    comboPooledDataSource.setUser(dbUser);
    comboPooledDataSource.setPassword(dbPassword);

    JdbcTemplate jdbcTemplate = new JdbcTemplate();
    jdbcTemplate.setLazyInit(false);
    jdbcTemplate.setDataSource(comboPooledDataSource);

    DbMachineIdProvider dbMachineIdProvider = new DbMachineIdProvider();
    dbMachineIdProvider.setJdbcTemplate(jdbcTemplate);
    dbMachineIdProvider.init();

    IdServiceImpl idServiceImpl = new IdServiceImpl();
    idServiceImpl.setMachineIdProvider(dbMachineIdProvider);
    if (genMethod != -1)
        idServiceImpl.setGenMethod(genMethod);
    if (type != -1)
        idServiceImpl.setType(type);
    if (version != -1)
        idServiceImpl.setVersion(version);
    idServiceImpl.init();

    return idServiceImpl;
}
 
Example 11
Source File: CustomerHandlerAuthentication.java    From CAS with Apache License 2.0 4 votes vote down vote up
@Override
protected AuthenticationHandlerExecutionResult doAuthentication(Credential credential) throws GeneralSecurityException, PreventedException {

    CustomCredential customCredential = (CustomCredential) credential;

    String username = customCredential.getUsername();
    String password = customCredential.getPassword();
    String email = customCredential.getEmail();
    String telephone = customCredential.getTelephone();
    String capcha = customCredential.getCapcha();

    ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    String right = attributes.getRequest().getSession().getAttribute("captcha_code").toString();


    if (!capcha.equalsIgnoreCase(right)) {
        throw new CheckCodeErrorException();
    }

    // 添加邮箱和电话的判断逻辑


    System.out.println("username : " + username);
    System.out.println("password : " + password);
    System.out.println("email : " + email);
    System.out.println("telephone : " + telephone);
    System.out.println("capcha : " + capcha);
    System.out.println("right : " + right);


    // JDBC模板依赖于连接池来获得数据的连接,所以必须先要构造连接池
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/cas");
    dataSource.setUsername("root");
    dataSource.setPassword("123");

    // 创建JDBC模板
    JdbcTemplate jdbcTemplate = new JdbcTemplate();
    jdbcTemplate.setDataSource(dataSource);

    String sql = "SELECT * FROM user WHERE username = ?";

    User info = (User) jdbcTemplate.queryForObject(sql, new Object[]{username}, new BeanPropertyRowMapper(User.class));

    System.out.println("database username : " + info.getUsername());
    System.out.println("database password : " + info.getPassword());


    if (info == null) {
        throw new AccountException("Sorry, username not found!");
    }

    if (!info.getPassword().equals(password)) {
        throw new FailedLoginException("Sorry, password not correct!");
    } else {

        final List<MessageDescriptor> list = new ArrayList<>();
        // 可自定义返回给客户端的多个属性信息
        HashMap<String, Object> returnInfo = new HashMap<>();
        returnInfo.put("expired", info.getDisabled());
        returnInfo.put("email", info.getEmail());
        returnInfo.put("username", info.getUsername());
        returnInfo.put("password", info.getPassword());
        returnInfo.put("disabled", info.getDisabled());

        return createHandlerResult(customCredential,
                this.principalFactory.createPrincipal(username, returnInfo), list);
    }


}
 
Example 12
Source File: CustomerHandlerAuthentication.java    From CAS with Apache License 2.0 4 votes vote down vote up
@Override
protected AuthenticationHandlerExecutionResult doAuthentication(Credential credential) throws GeneralSecurityException, PreventedException {

    CustomCredential customCredential = (CustomCredential) credential;

    String username = customCredential.getUsername();
    String password = customCredential.getPassword();
    String email = customCredential.getEmail();
    String telephone = customCredential.getTelephone();

    System.out.println("username : " + username);
    System.out.println("password : " + password);
    System.out.println("email : " + email);
    System.out.println("telephone : " + telephone);


    // JDBC模板依赖于连接池来获得数据的连接,所以必须先要构造连接池
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/cas");
    dataSource.setUsername("root");
    dataSource.setPassword("123");

    // 创建JDBC模板
    JdbcTemplate jdbcTemplate = new JdbcTemplate();
    jdbcTemplate.setDataSource(dataSource);

    String sql = "SELECT * FROM user WHERE username = ?";

    User info = (User) jdbcTemplate.queryForObject(sql, new Object[]{username}, new BeanPropertyRowMapper(User.class));

    System.out.println("database username : "+ info.getUsername());
    System.out.println("database password : "+ info.getPassword());


    if (info == null) {
        throw new AccountException("Sorry, username not found!");
    }

    if (!info.getPassword().equals(password)) {
        throw new FailedLoginException("Sorry, password not correct!");
    } else {

        final List<MessageDescriptor> list = new ArrayList<>();

        return createHandlerResult(customCredential,
                this.principalFactory.createPrincipal(username, Collections.emptyMap()), list);
    }


}
 
Example 13
Source File: CustomerHandlerAuthentication.java    From CAS with Apache License 2.0 4 votes vote down vote up
@Override
protected AuthenticationHandlerExecutionResult doAuthentication(Credential credential) throws GeneralSecurityException, PreventedException {

    CustomCredential customCredential = (CustomCredential) credential;

    String username = customCredential.getUsername();
    String password = customCredential.getPassword();
    String email = customCredential.getEmail();
    String telephone = customCredential.getTelephone();
    String capcha = customCredential.getCapcha();

    ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    String right = attributes.getRequest().getSession().getAttribute("captcha_code").toString();


    if (!capcha.equalsIgnoreCase(right)) {
        throw new CheckCodeErrorException();
    }

    // 添加邮箱和电话的判断逻辑


    System.out.println("username : " + username);
    System.out.println("password : " + password);
    System.out.println("email : " + email);
    System.out.println("telephone : " + telephone);
    System.out.println("capcha : " + capcha);
    System.out.println("right : " + right);


    // JDBC模板依赖于连接池来获得数据的连接,所以必须先要构造连接池
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/cas");
    dataSource.setUsername("root");
    dataSource.setPassword("123");

    // 创建JDBC模板
    JdbcTemplate jdbcTemplate = new JdbcTemplate();
    jdbcTemplate.setDataSource(dataSource);

    String sql = "SELECT * FROM user WHERE username = ?";

    User info = (User) jdbcTemplate.queryForObject(sql, new Object[]{username}, new BeanPropertyRowMapper(User.class));

    System.out.println("database username : " + info.getUsername());
    System.out.println("database password : " + info.getPassword());


    if (info == null) {
        throw new AccountException("Sorry, username not found!");
    }

    if (!info.getPassword().equals(password)) {
        throw new FailedLoginException("Sorry, password not correct!");
    } else {

        final List<MessageDescriptor> list = new ArrayList<>();
        // 可自定义返回给客户端的多个属性信息
        HashMap<String, Object> returnInfo = new HashMap<>();
        returnInfo.put("expired", info.getDisabled());
        returnInfo.put("email", info.getEmail());
        returnInfo.put("username", info.getUsername());
        returnInfo.put("password", info.getPassword());
        returnInfo.put("disabled", info.getDisabled());

        return createHandlerResult(customCredential,
                this.principalFactory.createPrincipal(username, returnInfo), list);
    }


}
 
Example 14
Source File: CustomerHandlerAuthentication.java    From CAS with Apache License 2.0 4 votes vote down vote up
@Override
protected AuthenticationHandlerExecutionResult doAuthentication(Credential credential) throws GeneralSecurityException, PreventedException {

    CustomCredential customCredential = (CustomCredential) credential;

    String username = customCredential.getUsername();
    String password = customCredential.getPassword();
    String email = customCredential.getEmail();
    String telephone = customCredential.getTelephone();
    String capcha = customCredential.getCapcha();

    ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    String right = attributes.getRequest().getSession().getAttribute("captcha_code").toString();


    if (!capcha.equalsIgnoreCase(right)) {
        throw new CheckCodeErrorException();
    }

    // 添加邮箱和电话的判断逻辑


    System.out.println("username : " + username);
    System.out.println("password : " + password);
    System.out.println("email : " + email);
    System.out.println("telephone : " + telephone);
    System.out.println("capcha : " + capcha);
    System.out.println("right : " + right);


    // JDBC模板依赖于连接池来获得数据的连接,所以必须先要构造连接池
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/cas");
    dataSource.setUsername("root");
    dataSource.setPassword("123");

    // 创建JDBC模板
    JdbcTemplate jdbcTemplate = new JdbcTemplate();
    jdbcTemplate.setDataSource(dataSource);

    String sql = "SELECT * FROM user WHERE username = ?";

    User info = (User) jdbcTemplate.queryForObject(sql, new Object[]{username}, new BeanPropertyRowMapper(User.class));

    System.out.println("database username : " + info.getUsername());
    System.out.println("database password : " + info.getPassword());


    if (info == null) {
        throw new AccountException("Sorry, username not found!");
    }

    if (!info.getPassword().equals(password)) {
        throw new FailedLoginException("Sorry, password not correct!");
    } else {

        final List<MessageDescriptor> list = new ArrayList<>();
        // 可自定义返回给客户端的多个属性信息
        HashMap<String, Object> returnInfo = new HashMap<>();
        returnInfo.put("expired", info.getDisabled());
        returnInfo.put("email", info.getEmail());
        returnInfo.put("username", info.getUsername());
        returnInfo.put("password", info.getPassword());
        returnInfo.put("disabled", info.getDisabled());

        return createHandlerResult(customCredential,
                this.principalFactory.createPrincipal(username, returnInfo), list);
    }


}
 
Example 15
Source File: CustomUsernamePasswordAuthentication.java    From CAS with Apache License 2.0 2 votes vote down vote up
@Override
protected AuthenticationHandlerExecutionResult authenticateUsernamePasswordInternal(UsernamePasswordCredential usernamePasswordCredential, String s) throws GeneralSecurityException, PreventedException {

    String username = usernamePasswordCredential.getUsername();

    String password = usernamePasswordCredential.getPassword();

    System.out.println("username : " + username);
    System.out.println("password : " + password);


    // JDBC模板依赖于连接池来获得数据的连接,所以必须先要构造连接池
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/cas");
    dataSource.setUsername("root");
    dataSource.setPassword("123");

    // 创建JDBC模板
    JdbcTemplate jdbcTemplate = new JdbcTemplate();
    jdbcTemplate.setDataSource(dataSource);

    String sql = "SELECT * FROM user WHERE username = ?";

    User info = (User) jdbcTemplate.queryForObject(sql, new Object[]{username}, new BeanPropertyRowMapper(User.class));

    System.out.println("database username : "+ info.getUsername());
    System.out.println("database password : "+ info.getPassword());



    if (info == null) {
        throw new AccountException("Sorry, username not found!");
    }

    if (!info.getPassword().equals(password)) {
        throw new FailedLoginException("Sorry, password not correct!");
    } else {

        // 可自定义返回给客户端的多个属性信息
        HashMap<String, Object> returnInfo = new HashMap<>();
        returnInfo.put("expired", info.getDisabled());

        final List<MessageDescriptor> list = new ArrayList<>();

        return createHandlerResult(usernamePasswordCredential,
                this.principalFactory.createPrincipal(username, returnInfo), list);
    }
}
 
Example 16
Source File: CustomerHandlerAuthentication.java    From CAS with Apache License 2.0 2 votes vote down vote up
@Override
protected AuthenticationHandlerExecutionResult doAuthentication(Credential credential) throws GeneralSecurityException, PreventedException {

    UsernamePasswordCredential usernamePasswordCredentia = (UsernamePasswordCredential) credential;

    String username = usernamePasswordCredentia.getUsername();
    String password = usernamePasswordCredentia.getPassword();

    System.out.println("username : " + username);
    System.out.println("password : " + password);


    // JDBC模板依赖于连接池来获得数据的连接,所以必须先要构造连接池
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/cas");
    dataSource.setUsername("root");
    dataSource.setPassword("123");

    // 创建JDBC模板
    JdbcTemplate jdbcTemplate = new JdbcTemplate();
    jdbcTemplate.setDataSource(dataSource);

    String sql = "SELECT * FROM user WHERE username = ?";

    User info = (User) jdbcTemplate.queryForObject(sql, new Object[]{username}, new BeanPropertyRowMapper(User.class));

    System.out.println("database username : "+ info.getUsername());
    System.out.println("database password : "+ info.getPassword());


    if (info == null) {
        throw new AccountException("Sorry, username not found!");
    }

    if (!info.getPassword().equals(password)) {
        throw new FailedLoginException("Sorry, password not correct!");
    } else {

        final List<MessageDescriptor> list = new ArrayList<>();

        return createHandlerResult(usernamePasswordCredentia,
                this.principalFactory.createPrincipal(username, Collections.emptyMap()), list);
    }


}
 
Example 17
Source File: CustomUsernamePasswordAuthentication.java    From CAS with Apache License 2.0 2 votes vote down vote up
@Override
protected AuthenticationHandlerExecutionResult authenticateUsernamePasswordInternal(UsernamePasswordCredential usernamePasswordCredential, String s) throws GeneralSecurityException, PreventedException {

    String username = usernamePasswordCredential.getUsername();

    String password = usernamePasswordCredential.getPassword();

    System.out.println("username : " + username);
    System.out.println("password : " + password);


    // JDBC模板依赖于连接池来获得数据的连接,所以必须先要构造连接池
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/cas");
    dataSource.setUsername("root");
    dataSource.setPassword("123");

    // 创建JDBC模板
    JdbcTemplate jdbcTemplate = new JdbcTemplate();
    jdbcTemplate.setDataSource(dataSource);

    String sql = "SELECT * FROM user WHERE username = ?";

    User info = (User) jdbcTemplate.queryForObject(sql, new Object[]{username}, new BeanPropertyRowMapper(User.class));

    System.out.println("database username : "+ info.getUsername());
    System.out.println("database password : "+ info.getPassword());



    if (info == null) {
        throw new AccountException("Sorry, username not found!");
    }

    if (!info.getPassword().equals(password)) {
        throw new FailedLoginException("Sorry, password not correct!");
    } else {

        // 可自定义返回给客户端的多个属性信息
        HashMap<String, Object> returnInfo = new HashMap<>();
        returnInfo.put("expired", info.getDisabled());

        final List<MessageDescriptor> list = new ArrayList<>();

        return createHandlerResult(usernamePasswordCredential,
                this.principalFactory.createPrincipal(username, returnInfo), list);
    }
}
 
Example 18
Source File: CustomUsernamePasswordAuthentication.java    From CAS with Apache License 2.0 2 votes vote down vote up
@Override
protected AuthenticationHandlerExecutionResult authenticateUsernamePasswordInternal(UsernamePasswordCredential usernamePasswordCredential, String s) throws GeneralSecurityException, PreventedException {

    String username = usernamePasswordCredential.getUsername();

    String password = usernamePasswordCredential.getPassword();

    System.out.println("username : " + username);
    System.out.println("password : " + password);


    // JDBC模板依赖于连接池来获得数据的连接,所以必须先要构造连接池
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/cas");
    dataSource.setUsername("root");
    dataSource.setPassword("123");

    // 创建JDBC模板
    JdbcTemplate jdbcTemplate = new JdbcTemplate();
    jdbcTemplate.setDataSource(dataSource);

    String sql = "SELECT * FROM user WHERE username = ?";

    User info = (User) jdbcTemplate.queryForObject(sql, new Object[]{username}, new BeanPropertyRowMapper(User.class));

    System.out.println("database username : "+ info.getUsername());
    System.out.println("database password : "+ info.getPassword());



    if (info == null) {
        throw new AccountException("Sorry, username not found!");
    }

    if (!info.getPassword().equals(password)) {
        throw new FailedLoginException("Sorry, password not correct!");
    } else {

        // 可自定义返回给客户端的多个属性信息
        HashMap<String, Object> returnInfo = new HashMap<>();
        returnInfo.put("expired", info.getDisabled());

        final List<MessageDescriptor> list = new ArrayList<>();

        return createHandlerResult(usernamePasswordCredential,
                this.principalFactory.createPrincipal(username, returnInfo), list);
    }
}
 
Example 19
Source File: CustomUsernamePasswordAuthentication.java    From CAS with Apache License 2.0 2 votes vote down vote up
@Override
protected AuthenticationHandlerExecutionResult authenticateUsernamePasswordInternal(UsernamePasswordCredential usernamePasswordCredential, String s) throws GeneralSecurityException, PreventedException {

    String username = usernamePasswordCredential.getUsername();

    String password = usernamePasswordCredential.getPassword();

    System.out.println("username : " + username);
    System.out.println("password : " + password);


    // JDBC模板依赖于连接池来获得数据的连接,所以必须先要构造连接池
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/cas");
    dataSource.setUsername("root");
    dataSource.setPassword("123");

    // 创建JDBC模板
    JdbcTemplate jdbcTemplate = new JdbcTemplate();
    jdbcTemplate.setDataSource(dataSource);

    String sql = "SELECT * FROM user WHERE username = ?";

    User info = (User) jdbcTemplate.queryForObject(sql, new Object[]{username}, new BeanPropertyRowMapper(User.class));

    System.out.println("database username : "+ info.getUsername());
    System.out.println("database password : "+ info.getPassword());



    if (info == null) {
        throw new AccountException("Sorry, username not found!");
    }

    if (!info.getPassword().equals(password)) {
        throw new FailedLoginException("Sorry, password not correct!");
    } else {

        // 可自定义返回给客户端的多个属性信息
        HashMap<String, Object> returnInfo = new HashMap<>();
        returnInfo.put("expired", info.getDisabled());

        final List<MessageDescriptor> list = new ArrayList<>();

        return createHandlerResult(usernamePasswordCredential,
                this.principalFactory.createPrincipal(username, returnInfo), list);
    }
}
 
Example 20
Source File: CustomerHandlerAuthentication.java    From CAS with Apache License 2.0 2 votes vote down vote up
@Override
protected AuthenticationHandlerExecutionResult doAuthentication(Credential credential) throws GeneralSecurityException, PreventedException {

    UsernamePasswordCredential usernamePasswordCredentia = (UsernamePasswordCredential) credential;

    String username = usernamePasswordCredentia.getUsername();
    String password = usernamePasswordCredentia.getPassword();

    System.out.println("username : " + username);
    System.out.println("password : " + password);


    // JDBC模板依赖于连接池来获得数据的连接,所以必须先要构造连接池
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/cas");
    dataSource.setUsername("root");
    dataSource.setPassword("123");

    // 创建JDBC模板
    JdbcTemplate jdbcTemplate = new JdbcTemplate();
    jdbcTemplate.setDataSource(dataSource);

    String sql = "SELECT * FROM user WHERE username = ?";

    User info = (User) jdbcTemplate.queryForObject(sql, new Object[]{username}, new BeanPropertyRowMapper(User.class));

    System.out.println("database username : "+ info.getUsername());
    System.out.println("database password : "+ info.getPassword());


    if (info == null) {
        throw new AccountException("Sorry, username not found!");
    }

    if (!info.getPassword().equals(password)) {
        throw new FailedLoginException("Sorry, password not correct!");
    } else {

        final List<MessageDescriptor> list = new ArrayList<>();

        return createHandlerResult(usernamePasswordCredentia,
                this.principalFactory.createPrincipal(username, Collections.emptyMap()), list);
    }


}