Spring Boot + Shiro + Thymleaf

时间:2017-01-06 18:42:31

标签: css spring

我的webapp存在一些问题。这是我的代码:

配置:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"app.controllers", "app.service"})
public class MainStConfig extends WebMvcConfigurerAdapter {
    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { 
            "classpath:/META-INF/resources/",
            "classpath:/resources/", 
            "classpath:/static/", 
            "classpath:/public/", 
            "classpath:/webjars/",
            "classpath:/tempplates/"
    };
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
    }



    @Bean(name = "shiroFilter")
    public ShiroFilterFactoryBean shiroFilter() {

        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setLoginUrl("/login.html");
        shiroFilter.setSuccessUrl("/index.html");
        shiroFilter.setUnauthorizedUrl("/index.html?error");


        Map<String, String> filterChain = new HashMap<>(); 
        filterChain.put("/", "anon");
        filterChain.put("/login", "authcBasic");
        filterChain.put("/logout", "logout");
        filterChain.put("/admin/**", "authc,roles[ADMIN]");
        filterChain.put("/student/**", "authc,roles[STUDENT]");
        filterChain.put("/teacher/**", "authc,roles[TEACHER]");
        //filterChain.put("/student/**", "authc,roles[STUDENT]");
        //filterChain.put("/teacher/**", "roles,roles[TEACHER]");

        shiroFilter.setFilterChainDefinitionMap(filterChain);
        shiroFilter.setSecurityManager(securityManager());


        Map<String, Filter> filters = new HashMap<>();
        filters.put("anon", new AnonymousFilter());
        filters.put("authc", new FormAuthenticationFilter());
        filters.put("logout", new LogoutFilter());
        filters.put("roles", new RolesAuthorizationFilter());
        filters.put("user", new UserFilter());
        shiroFilter.setFilters(filters);

        return shiroFilter;
    }
    @Bean
    public org.apache.shiro.mgt.SecurityManager securityManager() {

        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(userRealm());

        return securityManager;
    }
    @Bean(name = "userRealm")
    @DependsOn("lifecycleBeanPostProcessor")
    public UserRealm userRealm() {
        return new UserRealm();
    }
    @Bean
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

}

的IndexController:

@Controller
@RequestMapping("/")
public class IndexController {


    @RequestMapping(value = "/", method = RequestMethod.GET)
    String start() {
        return "index";
    }
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    String index() {
        return "index";
    }
    @RequestMapping("/login")
    String login() {
        return "login";
    }
}

的LoginController

@Controller
public class LoginController {
    private Session session;


    @ModelAttribute("userR")
    public User getUser() {
        return new User();
    }
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login() {
        return "login";
    }
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logoutt() {
        return "redirect:/index";
    }
    @RequestMapping(value = "/admin/index", method = RequestMethod.GET)
    public String admin() {
        return "admin/index";
    }
    @RequestMapping(value = "/student/index", method = RequestMethod.GET)
    public String student() {
        return "student/index";
    }
    @RequestMapping(value = "/teacher/index", method = RequestMethod.GET)
    public String teacher() {
        return "teacher/index";
    }


    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(Model model, @ModelAttribute("userR") User user, RedirectAttributes redirectAttrs, SessionStatus status) {
        Subject currentUser = SecurityUtils.getSubject();
        model.addAttribute("login", user.getLogin());

        if (StringUtils.hasText(user.getLogin()) && StringUtils.hasText(user.getPassword())) {
            try {
                UsernamePasswordToken token = new UsernamePasswordToken(user.getLogin(), user.getPassword());

                token.setRememberMe(true);
                currentUser.login(token);
                session = currentUser.getSession(false);

                if(currentUser.hasRole("ADMIN")) {
                    status.setComplete();
                    return "redirect:/admin/index";
                }
                if(currentUser.hasRole("STUDENT")) {
                    status.setComplete();
                    return "redirect:/student/index";
                }
                if(currentUser.hasRole("TEACHER")) {
                    status.setComplete();
                    return "redirect:/teacher/index";
                }
            } catch (Exception e) {
                return "login";
            }

            return "redirect:index";
        } else {
            return "login";
        }
    }
    @RequestMapping(value = "/logout", method = RequestMethod.POST)/*@RequestMapping(value = "/logout", method = RequestMethod.POST)*/
    public String logout() {
        Subject currentUser = SecurityUtils.getSubject();

        try {
            session.stop();
            currentUser.logout();
            return "redirect:/index";
        } catch (Exception e) {
            return "redirect:/index";
        }
    }
}

所以,这个代码总是当我在我的控制台中启动索引页面时,我可以看到没有布局的页面与css - 没有引导程序的白页但是当我改变这个方法时我是LoginController:

@RequestMapping(method = RequestMethod.POST)
    public String logout() {
        Subject currentUser = SecurityUtils.getSubject();

        try {
            session.stop();
            currentUser.logout();
            return "redirect:/index";
        } catch (Exception e) {
            return "redirect:/index";
        }
    }

为:

@RequestMapping(value = "/logout", method = RequestMethod.POST) 

然后一切正常,我可以看到所有颜色等,但现在这里有问题,当我尝试注销时,例如从/ admin / index然后我重定向到/ admin / logout,带有Whitable错误。应该像没有“value =”/ logout“”一样重定向到索引页面,因为它可以工作。

我使用fot退出按钮:

<form th:action="@{logout}" method="POST">
                <input type="submit" class="btn btn-info text-center center-block"
                    value="Wyloguj" />
            </form>

当我没有使用这个“value =”logout“”然后在控制台中注销后我可以看到这个警告:

2017-01-06 19:40:56.135  WARN 3400 --- [nio-8080-exec-6] o.s.web.servlet.PageNotFound             : Request method 'GET' not supported

我做错了什么?

0 个答案:

没有答案