授权概念
权限系统通常要解决认证和授权
- 认证,即在应用中谁能证明他就是他本人。一般提供如他们的身份ID一些标识信息来表明他就是他本人,如提供身份证,用户名/密码来证明。
- 授权,也叫访问控制,即在应用中控制谁能访问哪些资源(如访问页面/编辑数据/页面操作等)。
前面我们分析的都是认证,即你是谁问题?
这篇我们来分析你能做什么的问题。
授权分析
权限是某个资源能否杯用户访问,而并不是能否显示的问题。
权限场景
通常公司的系统分对外系统 权限场景比较简单,而内部管理系统权限场景比较复杂(业务及需求经常变动)
权限控制
权限控制是基于用户-角色-权限这套体系,这个我么后面详细介绍。其实我们前面的浏览器项目已经有权限控制了我们再来看下
com.rui.tiger.auth.browser.config.BrowserSecurityConfig#configure
-
/**
-
* 核心配置
-
* @param http
-
* @throws Exception
-
*/
-
@Override
-
protected void configure(HttpSecurity http) throws Exception {
-
/**
-
* 表单密码配置
-
*/
-
applyPasswordAuthenticationConfig(http);
-
http
-
.apply(captchaSecurityConfig)
-
.and()
-
.apply(smsAuthenticationSecurityConfig)
-
.and()
-
.apply(tigerSpringSocialConfigurer)
-
.and()
-
.rememberMe()
-
.tokenRepository(persistentTokenRepository())
-
.tokenValiditySeconds(securityProperties.getBrowser().getRemberMeSeconds())
-
.userDetailsService(userDetailsService)
-
.and()
-
.sessionManagement()
-
.invalidSessionStrategy(invalidSessionStrategy)
//session失效策略
-
.maximumSessions(securityProperties.getBrowser().getSession().getMaximumSessions())
//最大session并发数
-
.maxSessionsPreventsLogin(securityProperties.getBrowser().getSession().isMaxSessionsPreventsLogin())
//true达到并发数后阻止登录,false 踢掉之前的登录
-
.expiredSessionStrategy(sessionInformationExpiredStrategy)
//并发策略
-
.and()
-
.and()
-
.logout()
-
.logoutUrl(
"/loginOut")
//默认logout
-
//.logoutSuccessUrl("") url和Handler只能配置一个
-
.logoutSuccessHandler(tigerLogoutSuccessHandler)
-
.deleteCookies(
"JSESSIONID")
//清楚cook键值
-
.and()
-
.authorizeRequests()
-
.antMatchers(
-
SecurityConstants.DEFAULT_UNAUTHENTICATION_URL,
//权限认证
-
SecurityConstants.DEFAULT_LOGIN_PROCESSING_URL_MOBILE,
//手机
-
securityProperties.getBrowser().getLoginPage(),
//登录页面
-
SecurityConstants.DEFAULT_VALIDATE_CODE_URL_PREFIX+
"/*",
// /captcha/* 验证码放行
-
securityProperties.getBrowser().getSignupUrl(),
-
//这个第三方自定义权限 后续抽离出去 可配置
-
securityProperties.getBrowser().getLoginOut(),
-
"/user/regist",
-
"/index.html",
-
securityProperties.getBrowser().getSession().getInvalidSessionUrl())
-
.permitAll()
-
//底层会拼接ROLE_ADMIN
-
.antMatchers(
"/user").hasRole(
"ADMIN")
-
//restful风格匹配
-
.antMatchers(HttpMethod.GET,
"/user/*").hasRole(
"USER")
-
.anyRequest()
-
.authenticated()
-
.and()
-
.csrf().disable();
-
-
}
用户服务
这里只是模拟,通常用户的账号密码及相关权限信息,都是配置在数据中的。
-
private SocialUserDetails buildUser(String userId) {
-
// 根据用户名查找用户信息
-
//根据查找到的用户信息判断用户是否被冻结
-
String password = passwordEncoder.encode(
"123456");
-
log.info(
"数据库密码是:" + password);
-
return
new SocialUser(
-
userId,
-
password,
-
true,
-
true,
-
true,
-
true,
-
AuthorityUtils.commaSeparatedStringToAuthorityList(
"ROLE_ADMIN,ROLE_USER")
-
);
-
}
文章转载至:[https://blog.youkuaiyun.com/ahcr1026212/article/details/89855032](https://blog.youkuaiyun.com/ahcr1026212/article/details/89855032)