-1

我是 spring mvc 的新手,我正在开发一个 web 项目管理面板。

这是我的管理页面控制器的一些示例:

@Controller
@RequestMapping("/admin/article/**")
public class ArticleController {
    private ArticleDao articleDao;
    private String fileName;
    private String baseUrl;

    public ArticleController() {
        articleDao = ArticleDaoFactory.create();
    }
    @RequestMapping(value = "/admin/article",method = RequestMethod.GET)
    public String doGet(ModelMap model,HttpServletRequest request,ArticleForm articleForm) {
        //some codes
    }


    @RequestMapping(value = "/admin/article/add",method = RequestMethod.GET)
    public String doGetAdd(ModelMap model,ArticleForm articleForm) {
        model.addAttribute("article", articleForm);
        return "admin/articleAdd";
    }
    @RequestMapping(value = "/admin/article/add",method = RequestMethod.POST)
    public String doPost(@ModelAttribute ArticleForm article, BindingResult result ,ModelMap model){
        //some codes
    }

    @RequestMapping(value = "/admin/article/edit/{id}",method = RequestMethod.GET)
    public String getEdit(ModelMap model, @PathVariable("id") int id) {
        //some codes
    }

    @RequestMapping(value = "/admin/article/edit/{id}",method = RequestMethod.POST)
    public String postEdit(ModelMap model, @PathVariable("id") int id, ArticleForm article, BindingResult result) {
        //some codes
    }
    @RequestMapping(value = "/admin/article/delete/{id}",method = RequestMethod.GET)
    public void getDelete(ModelMap model, @PathVariable("id") int id, HttpServletResponse response) {
        //some codes
    }
}

现在我需要另一个名为 AdminController 的控制器中的另一个映射(例如)来验证 admin 并在他未登录的情况下将他带到登录页面。当然 Authenthication 就是一个例子,我可能想在每个管理页面上使用更多类。

请注意,我的身份验证类需要请求和会话引用(并且确保我的其他类将需要 spring 创建的其他引用)

我知道我无法获取 HttpServletRequest 并且......使用构造方法,所以我编写了另一个请求映射来调用方法。

尽管我可以这样设置我的属性,但我不能在每个管理 url 上使用这个方法。

@Controller
@RequestMapping(value = "/admin/**",method = RequestMethod.GET)
public class AdminController {
    Authentication authentication;
    HttpServletRequest request;
    HttpSession session;
    HttpServletResponse response;
    public void checkAndSet(HttpSession session,HttpServletRequest request,HttpServletResponse response) {
        authentication = new Authentication(session,request);
        this.request = request;
        this.session = session;
        this.response = response;
        if(!authentication.isLoggedIn()){
            System.out.println(" I'm not logged in");
            response.setHeader("Location","/admin/login");
        }

    }

所以我需要一些关于如何在控制器中编写请求映射以在所有其他“管理”页面子控制器上调用方法的建议?

仅供参考:我没有为此考虑春季安全。

谢谢;

4

1 回答 1

1

我认为您可以通过实现 servlet 过滤器来做到这一点。

例如 :

public class AuthenticationFilter extends GenericFilterBean {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        String url = request.getServletPath();
        HttpSession session = request.getSession(false);
        Authentication authentication = new Authentication(session,request);


        if (isAdminUrl(url) && !authentication.isLoggedIn()) {
            res.sendRedirect/admin/login");
        }
        chain.doFilter(req, res);
    }
}

然后,您必须实现该方法isAdminUrl(String url)以确定是否要应用过滤器。

否则,我强烈建议你看看Spring Security

于 2015-11-10T13:26:03.283 回答