spring boot security 实现根据情况跳转不同页面功能

在配置主类添加代码

@Override
        protected void configure(HttpSecurity http) throws Exception {
             http.authorizeRequests()
             .antMatchers(new String[]{"/js/**","/css/**","/picture/**","/images/**","/fonts/**","/**/favicon.ico"}).permitAll()
             .antMatchers("/home/*").permitAll()
             .anyRequest().authenticated()
            // .antMatchers(StaticParams.PATHREGX.NOAUTH,StaticParams.PATHREGX.CSS,StaticParams.PATHREGX.JS,StaticParams.PATHREGX.IMG).permitAll()//无需访问权限   
             //.antMatchers(StaticParams.PATHREGX.AUTHADMIN).hasAuthority(StaticParams.USERROLE.ROLE_ADMIN)//admin角色访问权限
             //.antMatchers(StaticParams.PATHREGX.AUTHUSER).hasAuthority(StaticParams.USERROLE.ROLE_USER)//user角色访问权限  StaticParams自定义枚举
             .and()
         .formLogin().successHandler(zhu())  //配置过滤器
             .loginPage("/login")
             .failureUrl("/login?error")
             //.defaultSuccessUrl("/equipment/getIndex", true)
             .permitAll()
             .and()
         .logout()
             .invalidateHttpSession(true) //是否清除Http session中的内容
             .permitAll().and()
         .csrf()                              //关闭csrf验证
             .disable();
        }
        
    @Bean
    public MyAuthenticationSuccessHandler zhu() {
        return new MyAuthenticationSuccessHandler();   //自写的security过滤器
    }
View Code

新建MyAuthenticationSuccessHandler 实现AuthenticationSuccessHandler接口

/**
 * 
 * security跳转过滤器
 * @author 苏俊源
 *
 */
@Component   //定义filter类
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler  {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication arg2)
            throws IOException, ServletException {
        // TODO Auto-generated method stub
     User user=(User)arg2.getPrincipal(); //获得登录用户信息
String f = request.getParameter("f"); //login前端页面表单中添加name为f的隐藏字段 if (StringUtils.isNotEmpty(f)) { if(f.equals("su")){ //response.setCharacterEncoding("UTF-8"); //response.getWriter().write("登录成功123"); response.sendRedirect("/"); } }else{ request.getRequestDispatcher("/").forward(request, response); } }

 

posted @ 2018-02-23 16:40  yhjjgj  阅读(780)  评论(0编辑  收藏  举报