gpt4 book ai didi

java - Spring阻止用户通过浏览器和ajax访问url

转载 作者:行者123 更新时间:2023-11-29 05:50:36 25 4
gpt4 key购买 nike

我在使用 Springs 安全配置时遇到了问题。我在下面分享我的代码。

我的问题是我想要 /user* url 和 /admin* url 应该只在用户登录到我的应用程序时访问,我的应用程序有主要的 ajax调用,所以我不希望任何用户在未登录的情况下访问 /user* URL。但是当我尝试在网络浏览器中键入 URL 时,我什至没有被重定向到登录页面,而是进入了在 URL 中输入的页面。

所以任何人都可以帮我解决这个问题。

spring-security.xml

    <?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">

<http auto-config="true">
<intercept-url pattern="/user**" access="ROLE_USER" />
<form-login login-page="/login" default-target-url="/user/home"
authentication-failure-url="/loginfailed" />
<logout invalidate-session="true" logout-success-url="/logout" />
</http>

<authentication-manager>
<authentication-provider>
<jdbc-user-service data-source-ref="dataSource"
users-by-username-query="select USERNAME, PASSWORD from USER where USERNAME = ?"
authorities-by-username-query="ROLE_USER"
/>
</authentication-provider>
</authentication-manager>

网络.xml

    <?xml version="1.0" encoding="utf-8" standalone="no"?><web-app 
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>
<servlet-name>Admin</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Admin</servlet-name>
<url-pattern>/admin*</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>User</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>User</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/User-servlet.xml,
/WEB-INF/Spring-Datasource.xml,
/WEB-INF/spring-security.xml
</param-value>
</context-param>

<!-- Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>

<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>index</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>SystemServiceServlet</servlet-name>
<servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
<init-param>
<param-name>services</param-name>
<param-value/>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SystemServiceServlet</servlet-name>
<url-pattern>/_ah/spi/*</url-pattern>
</servlet-mapping>
</web-app>

ControllerServlet.java

    @Controller
public class UserController {
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String getUserLoginPage(){
return "user/index";
}

@RequestMapping(value = "/loginfailed", method = RequestMethod.GET)
public String showErrorLoginPage(ModelMap modelMap){
modelMap.addAttribute("message", "Invalid Login Credentials");
return "user/index";
}

@RequestMapping(value = "/user/home", method = RequestMethod.GET)
public String getUserHomePage(ModelMap modelMap, Principal principal){
//String name = principal.getName();
//modelMap.addAttribute("name", name);
return "user/home";
}

@RequestMapping(value = "/logout", method = RequestMethod.GET)
public String showLogoutPage(ModelMap modelMap){
return "user/index";
}
}

数据库设计/代码:

    create table USER(ID BIGINT NOT NULL AUTO_INCREMENT, USERNAME varchar(20) NOT NULL UNIQUE, PASSWORD varchar(20) NOT NULL, FIRSTNAME varchar(25) NOT NULL, LASTNAME varchar(25) NOT NULL, UPDATED_ON varchar(25) NOT NULL, PRIMARY KEY (ID));

登录表单代码:

    <form class="form-horizontal" method="POST"
action="<c:url value='/j_spring_security_check' />" id="loginForm">
<div class="span4"></div>
<div class="span5" style="background-color: #FBFBFC; border: solid 1px #CCC;padding: 30px 5px 30px 5px;">
<div class="span1"></div>
<fieldset>
<legend>Login Here</legend>
<div class="control-group">
<label for="username">Username</label>
<input type="text" name="j_username" id="username"
placeholder="Username"
title="Please enter your username" data-placement="right" />
<div class="clear"></div>
<span id="errorSpan"></span>
</div>

<div class="control-group">
<label for="password">Password</label>
<input type="password" name="j_password" id="password"
placeholder="Password" title="Please enter your password"
data-placement="right" />
<div class="clear"></div>
<span id="errorSpan"></span>
</div>


<div class="control-group">
<label>&nbsp;</label>
<input type="submit" class="btn btn-primary" value="Sign In" />
</div>
<div class="span1"></div>
</fieldset>
</div>
<div class="span3"></div>
</form>

最佳答案

有两个问题:

首先,您必须更正地址模式:如 user/**

其次,如果您对安全资源进行了静态调用,则可能会发生这种情况。
检查 Firebug 。您会看到一个 302 重定向 作为对您请求的响应。在这种情况下,您最好不要使用重定向,而是给出一个 403 access denied 响应并在您的 ajax 框架中手动处理它。

关于java - Spring阻止用户通过浏览器和ajax访问url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14010646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com