Spring安全配置

时间:2016-04-02 13:59:56

标签: java spring hibernate spring-mvc spring-security

有人可以帮助我吗,我真的很累。我尝试将弹簧安全性添加到我的网站,我需要的是:swow pages dor 来宾 - index.jsp,用于用户 - user.jsp,basket.jsp,用于 admin - admin jsp。

我拥有:实体 - 用户和角色,DaoImplementation for:

1.User DAO

@Repository
public class UserDAOIMPL implements UsersDAO {
    @Autowired
    private SessionFactory sessionFactory;
    private Session openSession(){
         return  sessionFactory.getCurrentSession();
    }
    @Override
    public Users findByLogin(String login){
        List<Users> usersList = new ArrayList<>();
        Query query = openSession().createQuery("FROM  Users u where u.login  =:login ");
        query.setParameter("login", login);
        usersList = query.list();
        if (usersList.size()>0){
            return usersList.get(0);
        } else  return null;
    }
}

**

2.Role DAO

 @Repository
public class RoleDAOIMPL  implements  RoleDAO{
    @Autowired
    private SessionFactory sessionFactory;


    private Session  getCurrentSession(){
        return  sessionFactory.getCurrentSession();
    }

    @Override
    public Roles getRole(int id ){
        Roles role = (Roles) getCurrentSession().load(Roles.class, id);
        return role;
    }

}

第3。 MYUserService,用于将应用程序的User类映射到Spring Security的User类。

@Service
@Transactional(readOnly = true)
public class MyUserDetailService  implements UserDetailsService {

    @Autowired
    private UsersDAO usersDAO;

    public UserDetails loadUserByUsername(String login ) {

        Users domainUser = usersDAO.findByLogin(login);

        boolean enabled = true;
        boolean accountNonExpired = true;
        boolean credentialsNonExpired = true;
        boolean accountNonLocked = true;

        return new User(
                domainUser.getLogin(),
                domainUser.getPassword(),
                enabled,
                accountNonExpired,
                credentialsNonExpired,
                accountNonLocked,
                getAuthorities(domainUser.getRole().getId())
        );
    }

     public Collection getAuthorities(Integer role){
         List authList = getGrantedAuthorities((getRoleS(role)));
         return  authList;
    }

    public ArrayList<String> getRoleS(Integer role) {
        ArrayList<String> roles = new ArrayList();

        if (role.intValue() == 1) {
            roles.add("ROLE_MODERATOR");
            roles.add("ROLE_ADMIN");

        } else if (role.intValue() == 2) {
            roles.add("ROLE_MODERATOR");
        }

        return roles;

    }

    public static List getGrantedAuthorities(ArrayList<String> roles) {

        List   authorities = new ArrayList();
        for (String role : roles) {
            authorities.add(new SimpleGrantedAuthority(role));
        }
        return authorities;
    }
}

和弹簧安全配置,我现在尝试使用的最后一个:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

   @Autowired
   private DataSource dataSource;

    @Autowired
    private MyUserDetailService myUserDetailService;

    @Override
    protected void configure(HttpSecurity http)throws Exception {
        http.userDetailsService(myUserDetailService)
                .authorizeRequests()
                .antMatchers("/admin*//**" ).hasRole("ADMIN")
                 .antMatchers("/user").hasRole("USER")
                .and()
                .formLogin()
                .loginPage("/user-login.html")
                .defaultSuccessUrl("/success-login.html")
                .failureUrl("/error-login.html")
                .permitAll()
                .and()
                .logout()
                .logoutSuccessUrl("/index.html");
    }

}

我尝试了不同的方法,使用xml文件配置,但结果为零。如果项目开始,我有例外或者可以进入管理页面。

现在我得到例外:

https://github.com/Panwo/progProj/blob/master/1.txt

当我使用xml的安全性时,我可以访问我的所有页面而无需以管理员或用户身份登录。

0 个答案:

没有答案