2017-03-27 111 views
-1

我已經創建了一個授權過濾器之前調用其他API,但我得到404錯誤消息。以下是我創建的課程。澤西島客戶端API - 驗證錯誤使用澤西休息api

--RestAuthenticationFilter

package com.sms.Security; 

import java.io.IOException; 

import javax.servlet.FilterChain; 
import javax.servlet.FilterConfig; 
import javax.servlet.ServletException; 
import javax.servlet.ServletRequest; 
import javax.servlet.ServletResponse; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

public class RestAuthenticationFilter implements javax.servlet.Filter { 
    public static final String AUTHENTICATION_HEADER = "Authorization"; 

    @Override 
    public void doFilter(ServletRequest request, ServletResponse response, 
      FilterChain filter) throws IOException, ServletException { 
     if (request instanceof HttpServletRequest) { 
      HttpServletRequest httpServletRequest = (HttpServletRequest) request; 
      String authCredentials = httpServletRequest 
        .getHeader(AUTHENTICATION_HEADER); 

      // better injected 
      AuthenticationService authenticationService = new AuthenticationService(); 

      boolean authenticationStatus = authenticationService 
        .authenticate(authCredentials); 

      if (authenticationStatus) { 
       filter.doFilter(request, response); 
      } else { 
       if (response instanceof HttpServletResponse) { 
        HttpServletResponse httpServletResponse = (HttpServletResponse) response; 
        httpServletResponse 
          .setStatus(HttpServletResponse.SC_UNAUTHORIZED); 
       } 
      } 
     } 
    } 

    @Override 
    public void destroy() { 
    } 

    @Override 
    public void init(FilterConfig arg0) throws ServletException { 
    } 
} 

--AuthenticationService

package com.sms.Security; 

import java.io.IOException; 
import java.util.Base64; 
import java.util.StringTokenizer; 

public class AuthenticationService { 


    public boolean authenticate(String authCredentials) { 

     if (null == authCredentials) 
      return false; 
     // header value format will be "Basic encodedstring" for Basic 
     // authentication. Example "Basic YWRtaW46YWRtaW4=" 
     final String encodedUserPassword = authCredentials.replaceFirst("Basic" 
       + " ", ""); 
     String usernameAndPassword = null; 
     try { 
      byte[] decodedBytes = Base64.getDecoder().decode(
        encodedUserPassword); 
      usernameAndPassword = new String(decodedBytes, "UTF-8"); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     final StringTokenizer tokenizer = new StringTokenizer(
       usernameAndPassword, ":"); 
     final String username = tokenizer.nextToken(); 
     final String password = tokenizer.nextToken(); 

     // we have fixed the userid and password as admin 
     // call some UserService/LDAP here 
     boolean authenticationStatus = "admin".equals(username) 
       && "admin".equals(password); 
     return authenticationStatus; 
    } 
} 

--Web.xml文件

<web-app id="WebApp_ID" version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
    <display-name>Restful Web Application</display-name> 

    <servlet> 
     <servlet-name>jersey-serlvet</servlet-name> 
     <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> 
     <init-param> 
      <param-name>com.sun.jersey.config.property.packages</param-name> 
      <param-value>com.sms.presentationT</param-value> 
     </init-param> 

     <init-param> 
      <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> 
      <param-value>true</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>jersey-serlvet</servlet-name> 
     <url-pattern>/pages/rest/*</url-pattern> 
    </servlet-mapping> 

    <filter> 
    <filter-name>AuthenticationFilter</filter-name> 
    <filter-class>com.sms.Security.RestAuthenticationFilter</filter-class> 
    </filter> 

    <filter-mapping> 
    <filter-name>AuthenticationFilter</filter-name> 
    <url-pattern>/pages/rest/*</url-pattern> 
    </filter-mapping> 

</web-app> 

--login API

package com.sms.presentationT; 

import java.io.Serializable; 

import javax.annotation.security.RolesAllowed; 
import javax.ws.rs.Consumes; 
import javax.ws.rs.FormParam; 
import javax.ws.rs.GET; 
import javax.ws.rs.POST; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.MediaType; 
import javax.ws.rs.core.Response; 

import org.json.simple.JSONObject; 
import org.json.simple.parser.ParseException; 

import com.sms.userServices.*; 
import com.sms.Registrations.Regitration; 
import com.sms.persistenceT.*; 
import com.sms.persistenceT.*; 
import org.json.simple.parser.JSONParser; 
import org.json.simple.parser.ParseException; 
import com.sms.Security.*; 

@Path("/user-servicesLogincheck1") 
public class Check implements Serializable{ 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 


    @POST 
    @Path("/LoginPagecheck1") 

    //@Consumes("application/json") 
    //@Produces("application/json") 
    @Produces(MediaType.APPLICATION_JSON) 
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED) 
    public Regitration loginUserInfo(@FormParam("logusertype") String logUserType, 
           @FormParam("userNAME") String UserNAME, 
           @FormParam("PassWORD") String PAssWORD) throws ParseException 
    { 

     System.out.println(logUserType+UserNAME+PAssWORD); 

     CustomApplication CA = new CustomApplication(); 

     CA.getApplication(); 

     LoginUserServiceCheck LUS = new LoginUserServiceCheck(); 
     JSONObject jobj2 = new JSONObject(); 
     jobj2 = LUS.LoginInputDatacheck(logUserType,UserNAME, PAssWORD); 

     System.out.println("rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr"); 

     System.out.println("Presentation jobj"+jobj2); 


     String jsonText = jobj2.toJSONString(); 



     System.out.println(jsonText); 

     JSONParser parser = new JSONParser(); 

     JSONObject newJObject = null; 

     try { 
      newJObject = (JSONObject) parser.parse(jsonText); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 

     //System.out.println(LUS.loginresponse); 
     //System.out.println(LUS.resultStatus); 
     /* 
     if(LUS.resultStatus) 
     { 
      System.out.println(LUS.loginresponse); 


      return LUS.loginresponse; 
      //return "UserName is valid"; 
     } 
     else 
     { 
      return LUS.loginresponse; 
     } 

     */ 

     System.out.println(newJObject.get("Name")); 
     System.out.println(newJObject.get("Email")); 

     Regitration rg = new Regitration(); 

     rg.setUserType(logUserType); 
     rg.setName((String)newJObject.get("Name")); 
     rg.setEmail((String)newJObject.get("Email")); 
     rg.setUserName((String)newJObject.get("userName")); 
     rg.setPassword((String)newJObject.get("password")); 
     rg.setConfirmPasssword((String)newJObject.get("password")); 
     rg.setBirthDay((String)newJObject.get("birthday")); 
     rg.setGender((String)newJObject.get("Gender")); 
     rg.setMobileNo((String)newJObject.get("mobileNo")); 
     rg.setCurrentAddress((String)newJObject.get("currentAddress")); 

     rg.setPermenantAddress((String)newJObject.get("PermenantAddress")); 
     rg.setCountry((String)newJObject.get("Country")); 
     rg.setState((String)newJObject.get("state")); 
     rg.setCity((String)newJObject.get("City")); 

     rg.setLandmark((String)newJObject.get("Landmark")); 
     rg.setZipCode((String)newJObject.get("ZipCode")); 







     return rg; 
    } 

} 

- 錯誤消息

Mar 27, 2017 1:44:58 PM org.apache.catalina.startup.Catalina load 
INFO: Initialization processed in 2836 ms 
Mar 27, 2017 1:44:58 PM org.apache.catalina.core.StandardService start 
INFO: Starting service Catalina 
Mar 27, 2017 1:44:58 PM org.apache.catalina.core.StandardEngine start 
INFO: Starting Servlet Engine: Apache Tomcat/6.0.37 
Mar 27, 2017 1:44:59 PM org.apache.catalina.core.StandardContext filterStart 
SEVERE: Exception starting filter AuthenticationFilter 
java.lang.ClassNotFoundException: com.sms.Security.RestAuthenticationFilter 
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680) 
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526) 
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:269) 
    at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:424) 
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:115) 
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4072) 
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4726) 
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057) 
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:840) 
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057) 
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463) 
    at org.apache.catalina.core.StandardService.start(StandardService.java:525) 
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:754) 
    at org.apache.catalina.startup.Catalina.start(Catalina.java:595) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) 
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414) 
+0

看起來你的類不在你的戰爭中:'java.lang.ClassNotFoundException:com.sms.Security.RestAuthenticationFilter ' – Jens

回答

0

它看起來像RestAuthenticationFilter不可用在類路徑中,因此ClassNotFoundException的。請仔細檢查您是否正在編譯它並將其放入類路徑中。

+0

'請問你可以告訴我如何在類路徑中添加? –

+0

請參閱http://javahowto.blogspot.in/2006/06/6-ways-of-setting-java-classpath.html?m=1 – OTM