简体   繁体   中英

404 error after incorporationg spring security to my project

Hi I am learning how to filter requests with springs.Using spring security for it. After writing the spring-security.xml file in my project, I am getting a 404 error. Please help.

spring-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 file:

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 class :

     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;
      }

    }

Login page :

      <%@ 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?&nbsp;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>

I don't where I am going wrong.Please help.

您应该提供控制器,index.jsp和默认登录页面。以便一个人可以分析并帮助您解决问题。希望您尽快上传详细信息。

Looking into your code i found one mistake not related to your problem but still its need to be corrected for using the spring security for login you should use in login form

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM