Java Code Examples for org.springframework.jdbc.datasource.DriverManagerDataSource#setUrl()
The following examples show how to use
org.springframework.jdbc.datasource.DriverManagerDataSource#setUrl() .
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: TaleUtils.java From my-site with Apache License 2.0 | 6 votes |
/** * 获取新的数据源 * * @return */ public static DataSource getNewDataSource() { if (newDataSource == null) synchronized (TaleUtils.class) { if (newDataSource == null) { Properties properties = TaleUtils.getPropFromFile("application-default.properties"); if (properties.size() == 0) { return newDataSource; } DriverManagerDataSource managerDataSource = new DriverManagerDataSource(); managerDataSource.setDriverClassName("com.mysql.jdbc.Driver"); managerDataSource.setPassword(properties.getProperty("spring.datasource.password")); String str = "jdbc:mysql://" + properties.getProperty("spring.datasource.url") + "/" + properties.getProperty("spring.datasource.dbname") + "?useUnicode=true&characterEncoding=utf-8&useSSL=false"; managerDataSource.setUrl(str); managerDataSource.setUsername(properties.getProperty("spring.datasource.username")); newDataSource = managerDataSource; } } return newDataSource; }
Example 2
Source File: coreJDBC.java From Hands-On-High-Performance-with-Spring-5 with MIT License | 6 votes |
public static void main(String[] args) { DriverManagerDataSource dataSource = new DriverManagerDataSource(); //Postgres database we are using dataSource.setDriverClassName("org.postgresql.Driver"); dataSource.setUrl("jdbc:postgresql://localhost:5432/TestDB"); dataSource.setUsername("test"); dataSource.setPassword("test"); String query = "SELECT COUNT(*) FROM ACCOUNT"; try (Connection conn = dataSource.getConnection(); Statement statement = conn.createStatement(); ResultSet rsltSet = statement.executeQuery(query)) { if(rsltSet.next()){ int count = rsltSet.getInt(1); System.out.println("count : " + count); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
Example 3
Source File: MysqlAccountManagementService.java From wecube-platform with Apache License 2.0 | 5 votes |
public DriverManagerDataSource newMysqlDatasource(String host, String port, String username, String password, String database) { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); if(Strings.isNullOrEmpty(database)) { dataSource.setUrl("jdbc:mysql://" + host + ":" + port + "?characterEncoding=utf8&serverTimezone=UTC"); }else { dataSource.setUrl("jdbc:mysql://" + host + ":" + port +"/"+ database +"?characterEncoding=utf8&serverTimezone=UTC"); } dataSource.setUsername(username); dataSource.setPassword(password); return dataSource; }
Example 4
Source File: RepositoryConfig.java From Spring with Apache License 2.0 | 5 votes |
@Bean public DriverManagerDataSource getDataSource() { final DriverManagerDataSource ds = new DriverManagerDataSource(); ds.setDriverClassName(driverClassName); ds.setUrl(url); ds.setUsername(username); ds.setPassword(password); return ds; }
Example 5
Source File: RepositoryConfig.java From Spring with Apache License 2.0 | 5 votes |
@Bean public DriverManagerDataSource getDataSource() { final DriverManagerDataSource ds = new DriverManagerDataSource(); ds.setDriverClassName(driverClassName); ds.setUrl(url); ds.setUsername(username); ds.setPassword(password); return ds; }
Example 6
Source File: RepositoryConfig.java From Spring with Apache License 2.0 | 5 votes |
@Bean public DriverManagerDataSource getDataSource() { final DriverManagerDataSource ds = new DriverManagerDataSource(); ds.setDriverClassName(driverClassName); ds.setUrl(url); ds.setUsername(username); ds.setPassword(password); return ds; }
Example 7
Source File: LocalDataSourceConfig.java From cloud-espm-cloud-native with Apache License 2.0 | 5 votes |
/** * Returns local datasource * @return datasource */ @Bean public DataSource localDataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName(driverClassName); dataSource.setUrl(url); dataSource.setUsername(userName); dataSource.setPassword(password); return dataSource; }
Example 8
Source File: DataSourceConfig.java From Spring with Apache License 2.0 | 5 votes |
@Lazy @Bean(name = {"one", "two", "dataSource"}) public DataSource dataSource() { DriverManagerDataSource ds = new DriverManagerDataSource(); ds.setDriverClassName(driverClassName); ds.setUrl(url); ds.setUsername(username); ds.setPassword(password); return ds; }
Example 9
Source File: UserRepoDSConfig.java From Spring with Apache License 2.0 | 5 votes |
@Bean public DataSource dataSource() { DriverManagerDataSource ds = new DriverManagerDataSource(); ds.setDriverClassName(driverClassName); ds.setUrl(url); ds.setUsername(username); ds.setPassword(password); return ds; }
Example 10
Source File: CustomerHandlerAuthentication.java From CAS with Apache License 2.0 | 4 votes |
@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 11
Source File: CustomerHandlerAuthentication.java From CAS with Apache License 2.0 | 4 votes |
@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 |
@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 13
Source File: CustomerHandlerAuthentication.java From CAS with Apache License 2.0 | 4 votes |
@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 |
@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: CustomerHandlerAuthentication.java From CAS with Apache License 2.0 | 4 votes |
@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 16
Source File: CustomUsernamePasswordAuthentication.java From CAS with Apache License 2.0 | 2 votes |
@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 17
Source File: CustomUsernamePasswordAuthentication.java From CAS with Apache License 2.0 | 2 votes |
@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 |
@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 |
@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: CustomUsernamePasswordAuthentication.java From CAS with Apache License 2.0 | 2 votes |
@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); } }