《Spring实战》摘录 - 18
171
问题:#9.3-2 | spring中,用来定义如何保护路径的配置方法
回答:
- access(String) --- 如果给定的SpEL表达式计算结果为true,就允许访问
- anonymous() --- 允许匿名用户访问
- authenticated() --- 允许认证过的用户访问
- denyAll() --- 无条件拒绝所有访问
- fullyAuthenticated() --- 如果用户是完整认证的话(不是通过Remember-me功能认证的),就允许访问
- hasAnyAuthority(String...) --- 如果用户具备给定权限中的某一个的话,就允许访问
- hasAnyRole(String...) --- 如果用户具备给定角色中的某一个的话,就允许访问
- hasAuthority(String) --- 如果用户具备给定权限的话,就允许访问
- hasIpAddress(String) --- 如果请求来自给定IP地址的话,就允许访问
- hasRole(String) --- 如果用户具备给定角色的话,就允许访问
- not() --- 对其他访问方法的结果求反
- permitAll() --- 无条件允许访问
- rememberMe() --- 如果用户是通过Remember-me功能认证的,就允许访问
172
问题:#9.3.1 | Spring Security通过一些安全性相关的表达式扩展了Spring表达式语言
回答:
- authentication --- 用户的认证对象
- denyAll --- 结果始终为false
- hasAnyRole(list of roles) --- 如果用户被授予了列表中任意的指定角色,结果为true
- hasRole(role) --- 如果用户被授予了指定的角色,结果为true
- hasIpAddress(IP Address) --- 如果请求来自指定IP的话,结果为true
- isAnonymous() --- 如果当前用户为匿名用户,结果为true
- isAuthenticated() --- 如果当前用户进行了认证的话,结果为true
- isFullyAuthenticated() --- 如果当前用户进行了完整认证的话(不是通过Remember-me功能进行的认证),结果为true
- isRememberMe() --- 如果当前用户是通过Remember-me自动认证的,结果为true
- permitAll --- 结果始终为true
- principal --- 用户的principal对象
173
问题:#9.3.2-1 | requiresChannel()方法会为选定的URL强制使用HTTPS
回答:
@Override
protected void configure(httpSecurity http) throws exception {
http
. authorizeRequests()
.antMatchers("/spitter/me").hasRole("SPITTER")
.antMatchers(httpMethod.POST, "/spittles").hasRole("SPITTER")
.anyRequest().permitAll();
.and()
.requiresChannel()
.antMatchers("/spitter/form" ).requiresSecure(): //需要HTTPS
}
174
问题:#9.3.2-2 | 与之相反,有些页面并不需要通过HTTPS传送。例如,首页不包含任何敏感信息,因此并不需要通过HTTPS传送。我们可以使用requiresInsecure()代替requiresSecure()方法,将首页声明为始终通过HTTP传送。代码配置
回答:
.antMatchers("/").requiresInecure();
175
问题:#9.4.2-1 | HTTP Basic是什么?
回答: HTTP Basic认证(HTTP Basic Authentication)会直接通过HTTP请求本身,对要访问应用程序的用户进行认证
176
问题:#9.4.2-2 | 在Spring Security中启用HTTP Basic认证的典型配置
回答:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin()
.loginPage("/login")
.and()
.httpBasic()
.realmName("Spittr")
.and()
...
}
177
问题:#10.1.1-1 | JDBC过程中,可能导致抛出SQLException的常见问题包括:
回答:
- 应用程序无法连接数据库;
- 要执行的查询存在语法错误;
- 查询中所使用的表和/或列不存在;
- 试图插入或更新的数据违反了数据库约束。
178
问题:#10.1.2-1 | Spring将数据访问过程中固定的和可变的部分明确划分为两个不同的类,分别是:
回答:
- 模版, template,管理过程中固定的部分
- 回调, callback, 处理自定义的数据访问代码
179
问题:#10.1.2-2 | Spring提供的数据访问模板,分别适用于不同的持久化机制
回答:
- jca.cci.core.CciTemplate --- JCA CCI连接
- jdbc.core.JdbcTemplate --- JDBC连接
- jdbc.core.namedparam.NamedParameterJdbcTemplate --- 支持命名参数的JDBC连接
- jdbc.core.simple.SimpleJdbcTemplate --- 通过Java 5简化后的JDBC连接(Spring 3.1中已经废弃)
- orm.hibernate3.HibernateTemplate --- Hibernate 3.x以上的Session
- orm.ibatis.SqlMapClientTemplate --- iBATIS SqlMap客户端
- orm.jdo.JdoTemplate --- Java数据对象(Java Data Object)实现
- orm.jpa.JpaTemplate --- Java持久化API的实体管理器
180
问题:#10.2-1 | Spring提供了在Spring上下文中配置数据源bean的多种方式,包括:
回答:
- 通过JDBC驱动程序定义的数据源;
- 通过JNDI查找的数据源;
- 连接池的数据源