簡體   English   中英

通過數據庫與自定義用戶進行Spring安全認證

[英]Spring security authentication through database with custom user

我正在使用Spring安全性和注釋來通過數據庫和Ldap對用戶進行身份驗證。 詳細說明,由於Ldap不允許檢索屬性,我通過Ldap搜索檢查用戶(唯一代碼)和密碼是否正確,然后使用我的數據庫加載權限。 因此,我的數據庫中的所有用戶都存在於Ldap中,但如果用戶存在於Ldap而不存在於我的數據庫中,則會顯示特定頁面。 這是實際的代碼:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, proxyTargetClass = true)
@PropertySource(value = { "classpath:application.properties" })
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    AuthenticationConfiguration authenticationConfiguration;

    @Configuration
    protected static class AuthenticationConfiguration implements
    AuthenticationProvider {

        @Autowired
        private UserServices userServices;
        @Autowired
        LdapServices ldapServices;

        @Override
        public Authentication authenticate(Authentication authentication) throws AuthenticationException {
            Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
            String name = authentication.getName();
            String password = authentication.getCredentials().toString();
            boolean isFind = ldapServices.ldapSearch(name, password);                           
            if (isFind){
                com.domain.User user = userServices.getByUsersEnabled(name);
                if (user!=null)
                    authorities.add(new SimpleGrantedAuthority("ROLE_"+user.getRole().getRole()));          
                return new UsernamePasswordAuthenticationToken(name, password, authorities);
            }           
            else return null;
        }


        @Override
        public boolean supports(Class<?> authentication) {
            return authentication.equals(UsernamePasswordAuthenticationToken.class);
        }
    }

    @Autowired
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authenticationConfiguration);
        }

...web services authentication

我有簡單的用戶,我想添加一些信息,如姓名/電子郵件。 我讀到我必須實現UserDetailsUserDetailsService接口的loadUserByUsername ,但是如何將loadUserByUsername與我的代碼合並? 通過這種方式,我可以顯示姓名而不是用戶代碼。 謝謝

我更改了return new UsernamePasswordAuthenticationToken(name, password, authorities); return new UsernamePasswordAuthenticationToken(user, password, authorities); 在我的HTML頁面中,我使用sec:authentication="principal.name"來檢索名稱參數

我也遇到了類似問題的一些問題,然后我發現了一篇很棒的文章,幫助我完成了它。 文章在這里: spring-ldap-custom-authorities

我希望它有所幫助。 基本上,您必須在LDAP服務器上執行身份驗證過程,並且必須創建“ CustomLdapAuthoritiesPopulator ”,以便稍后獲取用戶詳細信息。

你必須在你的XML上有這樣的東西:

<beans:bean id="ldapAuthProvider"
    class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider">
    <beans:constructor-arg>
        <beans:bean
            class="org.springframework.security.ldap.authentication.BindAuthenticator">
            <beans:constructor-arg ref="contextSource" />
            <beans:property name="userSearch" ref="userSearch" />
        </beans:bean>
    </beans:constructor-arg>
    <beans:constructor-arg>
        <!-- User roles -->
        <beans:bean class="com.company.package.CustomLdapAuthoritiesPopulator" />
    </beans:constructor-arg>
</beans:bean>

稍后在您的CustomLdapAuthoritiesPopulator上,您將處理用戶角色。 像這樣的東西:

@Service("myAuthPopulator")
public class CustomLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator {
    @Transactional(readOnly=true)
    @Override
    public Collection<? extends GrantedAuthority> getGrantedAuthorities(DirContextOperations userData, String username) {

        Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
        try {

            User user = userService.findUserByUsername(username);

            if (user == null) {
               // User doesn't exist in the database
            } else {

               // user exists

                //get roles
                Set<UserRole> userRoles = user.getUserRoles();

                //add roles
                for (UserRole userRole : userRoles) {
                    authorities.add(new SimpleGrantedAuthority(userRole.getRole()));
                }

                return authorities;
            }
        } catch(Exception e) {
            //exception
        }
        return authorities;
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM