Spring+Spring security+tiles. How to set form-login with tiles page definition?

谁说我不能喝 提交于 2019-12-04 12:05:28

By default, there is nothing specific in the configuration for spring security with tiles.

If the name of your login page tile is loginPage, I guess you should have something similar to the following in your applicationContext-security.xml

<intercept-url pattern="/loginPage" filters="none"/>
<form-login login-page="/loginPage" />

If you are using spring security, the login page would have a POST url, which goes to /j_spring_security_check (by default). This is the equivalent of the loginController.

Another option is to have the following controller:

@Controller
@RequestMapping("/login")
public class LoginController {

    @RequestMapping(method = RequestMethod.GET)
    public String login(Locale locale, Model model) {
        return "login";
    }

    @RequestMapping(value = "/error", method = RequestMethod.GET)
    public String loginWithError(Locale locale, Model model) {
        model.addAttribute("error", true);
        return "login";
    }
}

In security-context.xml, configure form-login as follows:

<form-login login-page="/login" authentication-failure-url="/login/error" />

In views.xml declare the login view:

<definition name="login" extends="template">
    <put-attribute name="title" value="Nuevo Usuario"/>
    <put-attribute name="body" value="/login.jsp"/>
</definition>

finally, in login.jsp, to show the error in case there is one, use the following code:

<c:if test="${not empty error}">
    <font color="red"> Error al ingresar. <br /> Motivo :
        ${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}
    </font>
</c:if>

I've found a way to do it. In the spring config I've added this:

<bean id="loginController"
    class="org.springframework.web.servlet.mvc.ParameterizableViewController">
    <property name="viewName" value="loginPage" />
</bean>

And in the mapping accordingly add that:

<prop key="/login.htm">loginController</prop>

Tiles and security configurations remained the same.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!