码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-02-23 19:03:01      阅读:361      评论:0      收藏:0      [点我收藏+]

标签:实现   params   过滤器   icon   isa   ssh   odi   post   equal   

在配置主类添加代码

技术分享图片
@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
         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);  
                              
            }  
    }

 

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

标签:实现   params   过滤器   icon   isa   ssh   odi   post   equal   

原文地址:https://www.cnblogs.com/sujunyuan/p/8462653.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!