网站首页 > 技术文章 正文
认证
实现认证功能
认证功能需实现UserDetailsService接口
@Service
public class UserDetailServiceImpl implements UserDetailsService {
@Resource
private UserMapper userMapper;
@Override
public User loadUserByUsername(String username) throws UsernameNotFoundException {
if (StringUtils.isEmpty(username)) {
throw new UsernameNotFoundException("用户名不能为空!");
}
SysUser user = userMapper.selectByUsername(username);
if (user == null) {
throw new UsernameNotFoundException("用户名或密码错误!");
}
return new User(user.getAccount(), user.getPassword(), AuthorityUtils.createAuthorityList("edit,edit_or_update,ROLE_admin"));
}
}
说明:其中的重写的方法loadUserByUsername(String username)是认证的逻辑。
在返回的User对象中,第三个参数是定义用户的权限和角色。
注意:
- 定义用户角色的时候必须以ROLE_开头,比如ROLE_admin就定义了admin的角色。
而前面的edit和edit_or_update是用户的权限。
记住我
在SecurityConfig.class中加入以下配置:
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Resource
private DataSource dataSource;
@Bean
public PersistentTokenRepository jdbcTokenRepository() {
JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
jdbcTokenRepository.setDataSource(dataSource);
// 启动时是否创建记住我的配置表,首次启动可以开启,以后再启动需要关闭
// jdbcTokenRepository.setCreateTableOnStartup(true);
return jdbcTokenRepository;
}
@Override
public void configure(HttpSecurity httpSecurity) throws Exception {
// 设置记住我功能
httpSecurity.rememberMe()
// 设置前端给的记住我参数,值为boolean值,为true时记住我
.rememberMeParameter("rememberMe")
// 配置token仓库
.tokenRepository(jdbcTokenRepository())
// 设置多长时间内记住我,默认是两周
.tokenValiditySeconds(10080);
.userDetailsService(userDetailsService)
httpSecurity.csrf().disable();
}
注意:
- 如果登陆的时候返回的是自定义的UserDetails的实现类,一定要注意重写的方法String getUsername()返回值,这个值就是记住我存在数据库中username字段的值。
- userDetailsService()方法一定要设置,需要通过这个方法来进行登录验证。
退出登录
在SecurityConfig.class中加入以下配置:
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity httpSecurity) throws Exception {\
// 退出登录配置
httpSecurity.logout()
// 退出登录时请求的接口地址
.logoutUrl("/user/login_out")
// 退出登录成功后的处理器
.logoutSuccessHandler()
// 退出登录成功后跳转的url
.logoutSuccessUrl();
httpSecurity.csrf().disable();
}
}
具体代码实现可参考:https://gitee.com/gitwcx/walter-blog.git-> auth模块
猜你喜欢
- 2024-09-29 Spring Security 自定义登录过程(非前后端分离版本)
- 2024-09-29 基于spring-security图形验证码、token验证
- 2024-09-29 10分钟上手SpringSecurity 框架(3)
- 2024-09-29 SpringBoot 实现自动登录时的安全风险控制
- 2024-09-29 springboot+security框架整合 springboot security详解
- 2024-09-29 时序图说明JWT用户认证及接口鉴权的细节
- 2024-09-29 Spring Security 整合OAuth2 springsecurity整合oauth2+jwt+vue
- 2024-09-29 有关springboot + spring security的思考
- 2024-09-29 SpringSecurity之自定义用户权限信息的存取
- 2024-09-29 你还不了解SpringSecurity吗?快来看看SpringSecurity实战总结
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- oraclesql优化 (66)
- 类的加载机制 (75)
- feignclient (62)
- 一致性hash算法 (71)
- dockfile (66)
- 锁机制 (57)
- javaresponse (60)
- 查看hive版本 (59)
- phpworkerman (57)
- spark算子 (58)
- vue双向绑定的原理 (68)
- springbootget请求 (58)
- docker网络三种模式 (67)
- spring控制反转 (71)
- data:image/jpeg (69)
- base64 (69)
- java分页 (64)
- kibanadocker (60)
- qabstracttablemodel (62)
- java生成pdf文件 (69)
- deletelater (62)
- com.aspose.words (58)
- android.mk (62)
- qopengl (73)
- epoch_millis (61)
本文暂时没有评论,来添加一个吧(●'◡'●)