资讯专栏INFORMATION COLUMN

Spring4和SpringSecurity4的整合(二)连接mybatis和mysql

NoraXie / 1330人阅读

摘要:在上一篇基本配置了一些文件中,基本可以在文件中指定用户名和密码来进行实现的验证,这次和一起来配合使用加入的配置文件别名在的中配置数据源查找配置事物然后建立层,和层以及对应这里省略实

在上一篇基本配置了一些文件中,基本可以在文件中指定用户名和密码来进行实现SpringSecurity的验证,
这次和mynatis一起来配合使用

加入mybatis的配置文件:

mybatis-config.xml




    
    
        
    

在spring的ApplicationContext.xml中配置数据源
ApplicationContext.xml



    
    
    
        
        
        
        
    
    
        
        
        
    
    
        
        
    
    
    
        
    

然后建立dao层,和server层以及对应mapper(这里省略)
实现UserDetailService里面的loadUserByUsername方法

public class MyUserDetailService implements UserDetailsService  {
    @Autowired
    UserDao userdao;
    @Override
    public UserDetails loadUserByUsername(String username)
            throws UsernameNotFoundException {
        MyUser user =userdao.getUserByName(username);
        if(user==null)
        {
            throw new  UsernameNotFoundException("找不到该用户");
        }
//这里最好的做法就是遍历用户身份,获取用户权限
//        Collection grantedAuthorities = new ArrayList<>();
//        SimpleGrantedAuthority grantedAuthority = new SimpleGrantedAuthority(role);
//        grantedAuthorities.add(grantedAuthority);
        return new MyUserDetail(user, getAuthorities(user.getUser_role()));
    }

    private Collection getAuthorities(String role) {
        Collection grantedAuthorities = new ArrayList<>();
        SimpleGrantedAuthority grantedAuthority = new SimpleGrantedAuthority(role);
        grantedAuthorities.add(grantedAuthority);
        return grantedAuthorities;
    }

}

UserDetail

public class MyUserDetail implements UserDetails {
    private MyUser myUser;
    private Collection authorities;

    public MyUserDetail(MyUser user,Collection authorities) {
        this.myUser = user;
        this.authorities = authorities;
    }

    @Override
    public Collection getAuthorities() {
        // TODO Auto-generated method stub
        return authorities;
    }

    @Override
    public String getPassword() {
        return myUser.getUser_password();
    }

    @Override
    public String getUsername() {
        return myUser.getUser_name();
    }
//下面的方法可以以后再添加
    @Override
    public boolean isAccountNonExpired() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isAccountNonLocked() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isEnabled() {
        // TODO Auto-generated method stub
        return false;
    }

AuthenticationProvider类

关于类中的这个UsernamePasswordAuthenticationToken,Spring官方文档中给出的说明如下:
1. The username and password are obtained and combined into an instance of UsernamePasswordAuthenticationToken (an instance of
the Authentication interface, which we saw earlier).
2. The token is passed to an instance of AuthenticationManager for validation.
3. The AuthenticationManager returns a fully populated Authentication instance on successful authentication.
4. The security context is established by calling SecurityContextHolder.getContext().setAuthentication(… ) , passing in the returned
authentication object.
UsernamePasswordAuthenticationToken继承AbstractAuthenticationToken实现Authentication 所以当在页面中输入用户名和密码之后首先会进入到UsernamePasswordAuthenticationToken验证(Authentication),然后生成的Authentication会被交由AuthenticationManager来进行管理而AuthenticationManager管理一系列的AuthenticationProvider,而每一个Provider都会通UserDetailsService和UserDetail来返回一个以UsernamePasswordAuthenticationToken实现的带用户名和密码以及权限的Authentication
public class SecurityProvider implements AuthenticationProvider {
    @Autowired
    private MyUserDetailService userDetailsService;
    @Override
    public Authentication authenticate(Authentication authentication)
            throws AuthenticationException {
//
        UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) authentication;
        UserDetails userDetails = userDetailsService.loadUserByUsername(token.getName());
        if (userDetails == null) {
            throw new UsernameNotFoundException("找不到该用户");
        }
        if(!userDetails.getPassword().equals(token.getCredentials().toString()))
        {
              throw new BadCredentialsException("密码错误");
        }
        return new UsernamePasswordAuthenticationToken(userDetails,userDetails.getPassword(),userDetails.getAuthorities());
    }

    @Override
    public boolean supports(Class authentication) {
        // TODO Auto-generated method stub
        return UsernamePasswordAuthenticationToken.class.equals(authentication);
    }

}
项目地址:https://github.com/Somersames...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66718.html

相关文章

  • Spring4SpringSecurity4整合(一)

    摘要:的官方文档及其简单,他的示例配置就是在文件中把用户名和密码写固定了,然而在实际工作中是不可能的,参考了下网上的教程发现良莠不齐,特此写下记录学习过程首先导入包配置后面直接写这里会提示出错,提示找不 SpringSecurity的官方文档及其简单,他的示例配置就是在xml文件中把用户名和密码写固定了,然而在实际工作中是不可能的,参考了下网上的教程发现良莠不齐,特此写下记录学习过程首先po...

    sorra 评论0 收藏0
  • 基于注解方式配置springMVC 并整合mybatis()

    摘要:基于注解方式配置并整合一接上篇文章,如下是整合数据层。整合时,如果不加上就无法启动容器。 基于注解方式配置springMVC 并整合mybatis(一) 接上篇文章,如下是整合数据层。 spring-mybatis.xml ...

    peixn 评论0 收藏0
  • Nginx 搭建图片服务器

    摘要:搭建图片服务器本章内容通过和搭建图片服务器。第二个部分是为了更好的体验上传,批量上传,回显功能的富文本编辑器。总结搭建服务器的思维实现上传图片的功能上传图片的功能源码搭建图片服务器到这里就结束了,有什么不足的地方,请赐教。 Nginx 搭建图片服务器 本章内容通过Nginx 和 FTP 搭建图片服务器。在学习本章内容前,请确保您的Linux 系统已经安装了Nginx和Vsftpd。 N...

    jas0n 评论0 收藏0
  • 基于 SpringBoot2.0+优雅整合 SpringBoot+Mybatis

    摘要:基于最新的,是你学习的最佳指南。驱动程序通过自动注册,手动加载类通常是不必要。由于加上了注解,如果转账中途出了意外和的钱都不会改变。三的方式项目结构相比于注解的方式主要有以下几点改变,非常容易实现。公众号多篇文章被各大技术社区转载。 Github 地址:https://github.com/Snailclimb/springboot-integration-examples(Sprin...

    gghyoo 评论0 收藏0
  • SpringBoot2.0之五 优雅整合SpringBoot2.0+MyBatis+druid+Pa

    摘要:当禁用时,所有关联对象都会即时加载。不同的驱动在这方便表现不同。参考驱动文档或充分测试两种方法来决定所使用的驱动。需要适合的驱动。系统默认值是设置字段和类是否支持驼峰命名的属性。   上篇文章我们介绍了SpringBoot和MyBatis的整合,可以说非常简单快捷的就搭建了一个web项目,但是在一个真正的企业级项目中,可能我们还需要更多的更加完善的框架才能开始真正的开发,比如连接池、分...

    hatlonely 评论0 收藏0

发表评论

0条评论

NoraXie

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<