首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >《Spring实战》摘录 - 17

《Spring实战》摘录 - 17

作者头像
用户1335799
发布2019-05-15 14:33:54
4990
发布2019-05-15 14:33:54
举报

161

问题: #9.1.3-2 | Spring Security必须配置在一个什么样的bean中

回答: Spring Security必须配置在一个实现了WebSecurityConfigurer的bean中,或者(简单起见)扩展WebSecurityConfigurerAdapter

162

问题: #9.1.3-2 | 为Spring MVC启用Web安全性功能的最简单配置

回答:

package spitter.config:import org.springframework.context.annotation.Configuration;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.servlet.configuration.EnablewebMvcsecurity;
@Configuration@EnableWebMvcSecurity//启用SpringMVC安全性public class Securityconfig extends WebSecurityConfigurerAdapter {...}

163

问题: #9.1.3-3 | @EnableWebMvcSecurity注解还配置了一个Spring MVC参数解析解析器(argument resolver)。这么做的好处?

回答: 这样的话处理器方法就能够通过带有@AuthenticationPrincipal注解的参数获得认证用户的principal(或username)。它同时还配置了一个bean,在使用Spring表单绑定标签库来定义表单时,这个bean会自动添加一个隐藏的跨站请求伪造(cross-site request forgery,CSRF)token输入域。

164

问题: #9.1.3-4 | 重载WebSecurityConfigurerAdapter的configure()方法

回答:

  • configure(WebSecurity) --- 通过重载,配置Spring Security的Filter链
  • configure(HttpSecurity) --- 通过重载,配置如何通过拦截器保护请求
  • configure(AuthenticationManagerBuilder) --- 通过重载,配置user-detail服务

165

问题: #9.1.3-5 | Spring Security满足我们应用的需求,还需要再添加一点配置。具体来讲,需要什么?

回答:

  • 配置用户存储;
  • 指定哪些请求需要认证,哪些请求不需要认证,以及所需要的权限;
  • 提供一个自定义的登录页面,替代原来简单的默认登录页。

166

问题: #9.2.1-1 | 配置Spring Security使用内存用户存储的代码

回答:

package spitter.config:
import org.springframework.beans.factory.annotation.Autowired:import org.springframework.context.annotation.Configuration;import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity: 
@configuration@EnablewebMvcsecuritypublic class SecurityConfig extends WebSecurityConfigurerAdapter {    @Override    protected void configure( AuthenticationManagerBuilder auth) throws Exception(         auth        .inMemoryAuthentication()  //启用内存用户存储        .withuser("user").password("password").roles("USER").and()         .withUser("admin").password("password").roles("USER","ADMIN");    }}

167

问题: #9.2.1-2 | 配置用户详细信息的方法

回答:

  • accountExpired(boolean) --- 定义账号是否已经过期
  • accountLocked(boolean) --- 定义账号是否已经锁定
  • and() --- 用来连接配置
  • authorities(GrantedAuthority...) --- 授予某个用户一项或多项权限
  • authorities(List<? extends GrantedAuthority>) --- 授予某个用户一项或多项权限
  • authorities(String...) --- 授予某个用户一项或多项权限
  • credentialsExpired(boolean) --- 定义凭证是否已经过期
  • disabled(boolean) --- 定义账号是否已被禁用
  • password(String) --- 定义用户的密码
  • roles(String...) --- 授予某个用户一项或多项角色

168

问题: #9.2.1-3 | 为了配置Spring Security使用以JDBC为支撑的用户存储,所需的最少配置如下所示(代码)

回答:

@AutowiredDataSource dataSource;
@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {  auth    .jdbcAuthentication()    .dataSource(dataSource);}

169

问题: #9.2.4 | 假设我们需要认证的用户存储在非关系型数据库中,如Mongo或Neo4j,在这种情况下,我们需要提供一个自定义的UserDetailsService接口实现。其中userDetailService如何写?

回答:

public interface UserDetailsService {  UserDetails loadUserByUsername(String username) throws UsernameNotFoundException;}

170

问题: #9.3-1 | 对每个请求进行细粒度安全性控制的关键在于重载configure(HttpSecurity)方法。如下的代码片段展现了重载的configure(HttpSecurity)方法,它为不同的URL路径有选择地应用安全性

回答:

@Overrideprotected void configure(HttpSecurity http) throws Exception {  http    .authorizeRequests()    .antMatchers("/spitters/me").authenticated()    .antMatchers(HttpMethod.POST, "/spittles").authenticated()    .anyRequest().permitAll();}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-05-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 怀英的自我修炼 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
访问管理
访问管理(Cloud Access Management,CAM)可以帮助您安全、便捷地管理对腾讯云服务和资源的访问。您可以使用CAM创建子用户、用户组和角色,并通过策略控制其访问范围。CAM支持用户和角色SSO能力,您可以根据具体管理场景针对性设置企业内用户和腾讯云的互通能力。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档