您好我正在学习如何使用spring过滤请求。使用Spring安全性。在我的项目中编写spring-security.xml文件后,我收到404错误。请帮忙。
弹簧security.xml文件:
<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.2.xsd">
<!-- enable use-expressions -->
<http auto-config="true" use-expressions="true">
<!-- <intercept-url pattern="/*" access="hasRole('ROLE_ADMIN')" /> -->
<intercept-url pattern="/*" access="ROLE_MODERATOR" />
<!-- access denied page -->
<access-denied-handler error-page="/login" />
<form-login
login-page="/login" />
<logout logout-success-url="/login" />
<!-- enable csrf protection -->
<csrf />
</http>
<authentication-manager>
<authentication-provider user-service-ref="src/main/java/service/UserServiceDetailsImpl" >
<password-encoder hash="{sha}" />
</authentication-provider>
</authentication-manager>
</beans:beans>
web.xml文件:
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml,/WEB-INF/spring-security.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<welcome-file-list>
<welcome-file></welcome-file>
</welcome-file-list>
<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>
UserServiceDetailsImpl类:
package service;
import model.Login;
@Service
@Transactional(readOnly=true)
public class UserServiceDetailsImpl implements UserDetailsService {
@Autowired
private MusicPlayerServiceImpl2 service;
public UserDetails loadUserByUsername(String userName)
throws UsernameNotFoundException {
Login domainLogin = service.loadUserByUsername(userName);
boolean enabled = true;
boolean accountNonExpired = true;
boolean credentialsNonExpired = true;
boolean accountNonLocked = true;
return new User(
domainLogin.getUserName(),
domainLogin.getPassword(),
enabled,
accountNonExpired,
credentialsNonExpired,
accountNonLocked,
getAuthorities(domainLogin.getRole().getId())
);
}
public Collection<? extends GrantedAuthority> getAuthorities(Integer role) {
List<GrantedAuthority> authList = getGrantedAuthorities(getRoles(role));
return authList;
}
public List<String> getRoles(Integer role) {
List<String> roles = new ArrayList<String>();
if (role.intValue() == 1) {
roles.add("ROLE_MODERATOR");
roles.add("ROLE_ADMIN");
} else if (role.intValue() == 2) {
roles.add("ROLE_MODERATOR");
}
return roles;
}
public static List<GrantedAuthority> getGrantedAuthorities(List<String> roles) {
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
for (String role : roles) {
authorities.add(new GrantedAuthorityImpl(role));
}
return authorities;
}
}
登录页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css.map">
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="css/custom.css">
<script src="javascript/jquery.js"></script>
<script src="javascript/bootstrap.js"></script>
</head>
<body>
<div class="preview__header">
<div class="preview__envato-logo">
<h4>MusicPlayer</h4>
</div>
</div>
<div class="login_main_cont">
<div class="login_cont group login">
<div class="login_form modal-signup">
<h2>Login</h2>
<form method="post" name="login" action="login"
class="formClass">
<fieldset>
<input type="text" class="form-control focusedInput"
id="username" placeholder="username" name="username" required />
</fieldset>
<div class="spacer10"></div>
<fieldset>
<input type="password" class="form-control focusedInput"
id="loginPassword" placeholder="password" name="password"
required />
</fieldset>
<div class="spacer10"></div>
<fieldset class="login_submit">
<button type="submit" class="buton">SIGN IN</button>
</fieldset>
</form>
<div class="spacer10"></div>
<a class="link-sign" href="register" align="center" >Don't have
an account? Sign up!</a>
</div>
</div>
</div>
<!-- <div class="container" align="center">
<h4><strong>MusicPlayer</strong></h4>
<form role="form" method="post" name="login" action="loginServlet" class="form-inline" >
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control focusedInput" id="username" placeholder="username" name="username"/>
</div><br><div class="spacer10"></div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control focusedInput" id="loginPassword" placeholder="password" name="password" />
</div><br><div class="spacer10"></div>
<input type="submit" class="btn btn-default" id="login" value="Login" />
</form><div class="spacer10"></div>
<a href="register" class="btn btn-default" role="button">Register
</a> -->
</div>
<!-- <div class="w-container center">
<div class="modal-signup">
<div class="signup-form">
<form method="post" name="login" action="loginServlet"
class="form-inline">
<input type="text" class="w-input singup-field" id="username"
placeholder="username" name="username" required /> <input
type="password" class="w-input singup-field" id="password"
placeholder="password" name="password" required /> <input
class="w-button notify-btn sign-btn" type="submit"
value="sign in!" data-wait="Please wait..." id="login">
</form>
</div>
</div>
</div>-->
</body>
</html>
我不会在哪里出错。请帮助。
答案 0 :(得分:0)
您应该提供控制器,index.jsp和默认登录页面。这样一个人就可以分析并帮助您解决问题。我希望您能尽快上传详细信息。
答案 1 :(得分:0)
查看您的代码,我发现了一个与您的问题无关的错误,但仍需要更正使用登录时使用的登录弹簧安全性
<form name="login" action="<c:url value='j_spring_security_check'/>" method="POST" >
<input type="text" class="form-control" id="username" placeholder="Enter Username" name='j_username'>
<input type="password" class="form-control" id="password" placeholder="Password" name='j_password'>
</form>