成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

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

NoraXie / 1596人閱讀

摘要:在上一篇基本配置了一些文件中,基本可以在文件中指定用戶名和密碼來進(jìn)行實(shí)現(xiàn)的驗(yàn)證,這次和一起來配合使用加入的配置文件別名在的中配置數(shù)據(jù)源查找配置事物然后建立層,和層以及對(duì)應(yīng)這里省略實(shí)

在上一篇基本配置了一些文件中,基本可以在文件中指定用戶名和密碼來進(jìn)行實(shí)現(xiàn)SpringSecurity的驗(yàn)證,
這次和mynatis一起來配合使用

加入mybatis的配置文件:

mybatis-config.xml




    
    
        
    

在spring的ApplicationContext.xml中配置數(shù)據(jù)源
ApplicationContext.xml



    
    
    
        
        
        
        
    
    
        
        
        
    
    
        
        
    
    
    
        
    

然后建立dao層,和server層以及對(duì)應(yīng)mapper(這里省略)
實(shí)現(xiàn)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("找不到該用戶");
        }
//這里最好的做法就是遍歷用戶身份,獲取用戶權(quán)限
//        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類

關(guān)于類中的這個(gè)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實(shí)現(xiàn)Authentication 所以當(dāng)在頁面中輸入用戶名和密碼之后首先會(huì)進(jìn)入到UsernamePasswordAuthenticationToken驗(yàn)證(Authentication),然后生成的Authentication會(huì)被交由AuthenticationManager來進(jìn)行管理而AuthenticationManager管理一系列的AuthenticationProvider,而每一個(gè)Provider都會(huì)通UserDetailsService和UserDetail來返回一個(gè)以UsernamePasswordAuthenticationToken實(shí)現(xiàn)的帶用戶名和密碼以及權(quán)限的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("密碼錯(cuò)誤");
        }
        return new UsernamePasswordAuthenticationToken(userDetails,userDetails.getPassword(),userDetails.getAuthorities());
    }

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

}
項(xiàng)目地址:https://github.com/Somersames...

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/66718.html

相關(guān)文章

  • Spring4SpringSecurity4整合(一)

    摘要:的官方文檔及其簡(jiǎn)單,他的示例配置就是在文件中把用戶名和密碼寫固定了,然而在實(shí)際工作中是不可能的,參考了下網(wǎng)上的教程發(fā)現(xiàn)良莠不齊,特此寫下記錄學(xué)習(xí)過程首先導(dǎo)入包配置后面直接寫這里會(huì)提示出錯(cuò),提示找不 SpringSecurity的官方文檔及其簡(jiǎn)單,他的示例配置就是在xml文件中把用戶名和密碼寫固定了,然而在實(shí)際工作中是不可能的,參考了下網(wǎng)上的教程發(fā)現(xiàn)良莠不齊,特此寫下記錄學(xué)習(xí)過程首先po...

    sorra 評(píng)論0 收藏0
  • 基于注解方式配置springMVC 并整合mybatis()

    摘要:基于注解方式配置并整合一接上篇文章,如下是整合數(shù)據(jù)層。整合時(shí),如果不加上就無法啟動(dòng)容器。 基于注解方式配置springMVC 并整合mybatis(一) 接上篇文章,如下是整合數(shù)據(jù)層。 spring-mybatis.xml ...

    peixn 評(píng)論0 收藏0
  • Nginx 搭建圖片服務(wù)器

    摘要:搭建圖片服務(wù)器本章內(nèi)容通過和搭建圖片服務(wù)器。第二個(gè)部分是為了更好的體驗(yàn)上傳,批量上傳,回顯功能的富文本編輯器??偨Y(jié)搭建服務(wù)器的思維實(shí)現(xiàn)上傳圖片的功能上傳圖片的功能源碼搭建圖片服務(wù)器到這里就結(jié)束了,有什么不足的地方,請(qǐng)賜教。 Nginx 搭建圖片服務(wù)器 本章內(nèi)容通過Nginx 和 FTP 搭建圖片服務(wù)器。在學(xué)習(xí)本章內(nèi)容前,請(qǐng)確保您的Linux 系統(tǒng)已經(jīng)安裝了Nginx和Vsftpd。 N...

    jas0n 評(píng)論0 收藏0
  • 基于 SpringBoot2.0+優(yōu)雅整合 SpringBoot+Mybatis

    摘要:基于最新的,是你學(xué)習(xí)的最佳指南。驅(qū)動(dòng)程序通過自動(dòng)注冊(cè),手動(dòng)加載類通常是不必要。由于加上了注解,如果轉(zhuǎn)賬中途出了意外和的錢都不會(huì)改變。三的方式項(xiàng)目結(jié)構(gòu)相比于注解的方式主要有以下幾點(diǎn)改變,非常容易實(shí)現(xiàn)。公眾號(hào)多篇文章被各大技術(shù)社區(qū)轉(zhuǎn)載。 Github 地址:https://github.com/Snailclimb/springboot-integration-examples(Sprin...

    gghyoo 評(píng)論0 收藏0
  • SpringBoot2.0之五 優(yōu)雅整合SpringBoot2.0+MyBatis+druid+Pa

    摘要:當(dāng)禁用時(shí),所有關(guān)聯(lián)對(duì)象都會(huì)即時(shí)加載。不同的驅(qū)動(dòng)在這方便表現(xiàn)不同。參考驅(qū)動(dòng)文檔或充分測(cè)試兩種方法來決定所使用的驅(qū)動(dòng)。需要適合的驅(qū)動(dòng)。系統(tǒng)默認(rèn)值是設(shè)置字段和類是否支持駝峰命名的屬性。 ??上篇文章我們介紹了SpringBoot和MyBatis的整合,可以說非常簡(jiǎn)單快捷的就搭建了一個(gè)web項(xiàng)目,但是在一個(gè)真正的企業(yè)級(jí)項(xiàng)目中,可能我們還需要更多的更加完善的框架才能開始真正的開發(fā),比如連接池、分...

    hatlonely 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<