2017-06-15 41 views
0

我使用spring mvc + spring boot hibernate。在我運行應用程序之前突然出現這個錯誤。下面是錯誤mesage得到錯誤java:找不到符號類

C:\用戶\ User_2 \ PDRM \ SRC \主\的java \ COM \示例\ RegisterController.java 錯誤:(23,19)的java:無法找到符號 符號:類Default_profilesRepository 位置:包com。示例 錯誤:(33,13)的java:無法找到符號 符號:類Default_profilesRepository 位置:類com.example.RegisterController

這裏RegisterController

package com.example; 

    import com.example.*; 
    import org.springframework.beans.factory.annotation.Autowired; 
    import org.springframework.stereotype.Controller; 
    import org.springframework.ui.Model; 
    import org.springframework.validation.BindingResult; 
    import org.springframework.validation.Errors; 
    import org.springframework.web.bind.annotation.*; 
    import org.springframework.web.context.request.WebRequest; 

    import javax.validation.Valid; 
    import java.io.UnsupportedEncodingException; 
    import java.math.BigInteger; 
    import java.security.MessageDigest; 
    import java.security.NoSuchAlgorithmException; 
    import java.util.Date; 

    import java.util.UUID; 

    import static jdk.nashorn.internal.objects.NativeString.substr; 

    @Controller 

    public class RegisterController { 

    @Autowired 
    private Default_profilesRepository profilesRepository; 

@Autowired 
private Default_usersRepository usersRepository; 

@RequestMapping("/login") 
public String Login(Model model) 
{ 
    model.addAttribute("user", new Default_users()); 

    return "login"; 
} 

@RequestMapping("/loginProcess") 
public String loginProcess(@ModelAttribute(value="user") @Valid Default_users user, BindingResult bindingResultUser) 
{ 
    if(bindingResultUser.hasErrors()) 
    { 
     return "login"; 
    } 

    Default_users user2 = usersRepository.findByEmail(user.getEmail()); 
    if (user2 != null) { 


     String passwordToHash = user.getPassword(); 
     String saltDB = user2.getSalt(); 
     String Password = null; 
     try { 
      // Create MessageDigest instance for MD5 
      MessageDigest md = MessageDigest.getInstance("MD5"); 
      //Add password bytes to digest 
      md.update(passwordToHash.getBytes()); 
      //Get the hash's bytes 
      byte[] bytes = md.digest(); 
      //This bytes[] has bytes in decimal format; 
      //Convert it to hexadecimal format 
      StringBuilder sb = new StringBuilder(); 
      for (int i = 0; i < bytes.length; i++) { 
       sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1)); 
      } 
      //Get complete hashed password in hex format 

     } catch (NoSuchAlgorithmException e) { 
      e.printStackTrace(); 
     } 
     String uuid = UUID.randomUUID().toString(); 
     MessageDigest crypt = null; 
     try { 
      crypt = MessageDigest.getInstance("SHA-1"); 
     } catch (NoSuchAlgorithmException e) { 
      e.printStackTrace(); 
     } 
     crypt.reset(); 
     try { 
      String tets = passwordToHash + saltDB; 
      crypt.update(tets.getBytes("UTF-8")); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } 
     Password = new BigInteger(1, crypt.digest()).toString(16); 
     System.out.println(user2.getPassword()); 
     System.out.println(Password); 
     System.out.println(saltDB); 

     if (Password.equals(user2.getPassword())) { 


      return "/home"; 
     } else { 
      return "/login"; 
     } 
    } 
    return "/home"; 
} 

@GetMapping("/register") 
public String Register(Model model) 

{ 
    model.addAttribute("profile", new Default_profiles()); 
    model.addAttribute("user", new Default_users()); 

    return "register"; 
} 

@RequestMapping("/save") 
public String Process(@ModelAttribute(value="user") @Valid Default_users user, BindingResult bindingResultUser, WebRequest request, Errors errors, @ModelAttribute(value="profile") @Valid Default_profiles profile, BindingResult bindingResultProfile) 

{ 

    Date date = new Date(); 
    int unixTime = (int) date.getTime()/1000; 

    Default_users userExists = usersRepository.findByEmail(user.getEmail()); 
    System.out.println(userExists); 
    if (userExists != null) { 
     bindingResultUser 
       .rejectValue("email", "error.user", 
         "There is already a user registered with the email provided"); 
    } 
    if(bindingResultUser.hasErrors() || bindingResultProfile.hasErrors()) 
    { 
     return "register"; 
    } 

    String passwordToHash = user.getPassword(); 
    String salt = null; 
    String Password = null; 
    try { 
     // Create MessageDigest instance for MD5 
     MessageDigest md = MessageDigest.getInstance("MD5"); 
     //Add password bytes to digest 
     md.update(passwordToHash.getBytes()); 
     //Get the hash's bytes 
     byte[] bytes = md.digest(); 
     //This bytes[] has bytes in decimal format; 
     //Convert it to hexadecimal format 
     StringBuilder sb = new StringBuilder(); 
     for (int i = 0; i < bytes.length; i++) { 
      sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1)); 
     } 
     //Get complete hashed password in hex format 
     salt = sb.toString(); 
     salt = substr(salt, 0, 6); 
    } catch (NoSuchAlgorithmException e) { 
     e.printStackTrace(); 
    } 
    String uuid = UUID.randomUUID().toString(); 
    MessageDigest crypt = null; 
    try { 
     crypt = MessageDigest.getInstance("SHA-1"); 
    } catch (NoSuchAlgorithmException e) { 
     e.printStackTrace(); 
    } 
    crypt.reset(); 
    try { 
     String tets = passwordToHash + salt; 
     crypt.update(tets.getBytes("UTF-8")); 
    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } 
    Password = new BigInteger(1, crypt.digest()).toString(16); 
    user.setPassword(Password); 
    user.setSalt(salt); 
    user.setGroup_id(2); 
    user.setIp_address(""); 
    user.setActive(1); 
    user.setActivation_code(""); 
    user.setCreated_on(unixTime); 
    user.setLast_login(unixTime); 
    user.setForgotten_password_code(""); 
    user.setRemember_code("hgshd"); 

    profile.setCreated(date); 
    profile.setUpdated(date); 
    profile.setCreated_by(unixTime); 
    profile.setOrdering_count(0); 
    profile.setDisplay_name(user.getEmail()); 
    profile.setDob(0); 
    profile.setGender(""); 
    profile.setPhone(""); 
    profile.setAddress_line1(""); 
    profile.setAddress_line2(""); 
    profile.setAddress_line3(""); 
    profile.setPostcode(""); 
    profile.setUpdated_on(0); 
    profile.setCountry("MY"); 
    profile.setUser_id(user); 

    profilesRepository.save(profile); 

    return "/result"; 

     } 

    `` } 

這裏Default_profilesRepository

package com.example; 

import org.springframework.data.repository.CrudRepository; 


    public interface Default_profilesRepository extends 
    CrudRepository<Default_profiles, Integer> { 


    } 

配置文件

package com.example; 


import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Qualifier; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 
    import org.springframework.security.config.annotation.web.builders.HttpSecurity; 
    import org.springframework.security.config.annotation.web.builders.WebSecurity; 
    import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 
    import org.springframework.security.web.util.matcher.AntPathRequestMatcher; 

    import javax.sql.DataSource; 

    @Configuration 
    @EnableWebSecurity 
    @ComponentScan 
    public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Qualifier("dataSource") 
@Autowired 
private DataSource dataSource; 

@Autowired 
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception { 

    auth.jdbcAuthentication().dataSource(dataSource) 
      .usersByUsernameQuery(
        "select u from Default_users u where u.email=?"); 
} 


@Override 
protected void configure(HttpSecurity http) throws Exception { 

    http. 
      authorizeRequests() 
      .antMatchers("/").permitAll() 
      .antMatchers("/login").permitAll() 
      .antMatchers("/registration").permitAll() 
      .anyRequest().authenticated() 
      .and() 
      .formLogin() 
      .and().csrf().disable().formLogin() 
      .loginPage("/login").failureUrl("/login?error=true") 
      .defaultSuccessUrl("/home") 
      .and().logout() 
      .logoutRequestMatcher(new AntPathRequestMatcher("/logout")) 
      .logoutSuccessUrl("/").and().exceptionHandling() 
      .accessDeniedPage("/access-denied"); 

    http.csrf().disable(); 
    http.headers().frameOptions().disable(); 

} 

@Override 
public void configure(WebSecurity web) throws Exception { 
    web 
      .ignoring() 
      .antMatchers("/resources/**", "/static/**", "/css/**", "/js/**", "/images/**"); 
} 

}

主要應用

package com.example; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.cache.annotation.EnableCaching; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 

    @SpringBootApplication 
    @Configuration 
    @EnableCaching 

    public class PdrmApplication { 

    public static void main(String[] args) { 
    SpringApplication.run(PdrmApplication.class, args); 
    } 
    } 

回答

0

我通過爲每個圖層創建包來解決了這個問題。

  • com.example.model
  • com.example.repository
  • com.example.controller
  • com.example.configuration
0

可能有這種錯誤的各種原因。像丟失或錯誤的組件 - 掃描標籤...

你能不能請你分享你的spring配置文件。

+0

爲什麼userRepository不要錯誤?只有profilerepository有錯誤 – styles

+0

我想如果你評論profilerepository,那麼它會顯示爲userRepository。 您可以嘗試添加基本軟件包以掃描componentscan註釋,或者您可以使用springbootapplication註釋添加軟件包。 –

+0

如果我評論profilerepository,我可以運行該應用程序。我在主應用程序中添加@ComponentScan(basePackages =「com.example」)。仍然有同樣的錯誤 – styles