import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class HttpClientExample { public static void main(String[] args) { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("http://api.example.com"); try { CloseableHttpResponse response = httpClient.execute(httpGet); String responseBody = EntityUtils.toString(response.getEntity()); System.out.println(responseBody); } catch (Exception e) { e.printStackTrace(); } finally { try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } } } import org.springframework.jdbc.core.JdbcTemplate; import javax.sql.DataSource; public class SpringJDBCExample { private JdbcTemplate jdbcTemplate; public SpringJDBCExample(DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); } public void queryData() { String sql = "SELECT * FROM users"; List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql); for (Map<String, Object> row : rows) { System.out.println(row.get("name")); } } } @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("admin").password("{noop}admin").roles("ADMIN") .and() .withUser("user").password("{noop}user").roles("USER"); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/admin/**").hasRole("ADMIN") .antMatchers("/user/**").hasAnyRole("ADMIN", "USER") .and().formLogin(); } }


上一篇:
下一篇:
切换中文