내가 그래서 몇 가지 튜토리얼을 흘러 봄 보안을 사용하여 실시 예에 로그를 구현하기 위해 노력하고 내가 지금까지이 코드를 작성했습니다 :인증 요청이 실패했습니다 : org.springframework.security.authentication.BadCredentialsException : 잘못된 자격 증명
역할. 자바 :
@Entity
public class Role {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String roleName;
@ManyToMany(mappedBy="roles")
private List<User> users;
public Role(){}
public Role(int id, String roleName, List<User> users) {
super();
this.id = id;
this.roleName = roleName;
this.users = users;
//all setter and getters
}
User.java
@Entity
@Table(name = "User")
public class User implements Serializable{
@Autowired
private SessionFactory factory;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String username;
private String passowrd;
@ManyToMany
@JoinTable(name="UserAndRoles",
[email protected](name="user_id"),
[email protected](name="role_id"))
private List<Role> roles;
@Enumerated(EnumType.STRING)
private UserStatus status;
// 생성자와 세터, 게터
이}
다오 분류 :
public interface UserDao {
void addUser(User user);
void editUser(User user);
void deleteUser(int userId);
User findUser(int userId);
User findUserByName(String username);
List<User> getAllUsers();
}
DAO 구현 :
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private SessionFactory session = HibernateUtil.getSessionFactory();
@Override
public void addUser(User user) {
session.getCurrentSession().save(user);
}
//all methods of Dao Class
}
및 UserDetailsService의 클래스 :
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
private UserDao userDao;
@Override
@Transactional(readOnly = true)
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException {
User user=userDao.findUserByName(username);
if(user!=null)
{
String password=user.getPassowrd();
boolean enabled=user.getStatus().equals(UserStatus.ACTIVE);
boolean accountNonExpired = user.getStatus().equals(UserStatus.ACTIVE);
boolean credentialsNonExpired = user.getStatus().equals(UserStatus.ACTIVE);
boolean accountNonLocked = user.getStatus().equals(UserStatus.ACTIVE);
//populate user roles
Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
for(Role role : user.getRoles()){
authorities.add(new GrantedAuthorityImpl(role.getRoleName()));
}
//create spring security object
org.springframework.security.core.userdetails.User securityUser = new
org.springframework.security.core.userdetails.User(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
return securityUser;
}else {
throw new UsernameNotFoundException("user not found !!!");
}
}
}
스프링 security.xml :
마지막으로 봄-servlet.xml에 : 나는이 성공적으로 실행 작성 있지만 로그인 자격 증명을받지되는 코드 위
<!-- Hibernate Configurations -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/portal"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
.
내가 만든 INT 스프링 보안의 로그인 양식을 사용하고 내가라는 이름으로 DB에있는 세 개의 테이블 만들었습니다
사용자, 역할, userAndRoles
을 여전히 나는 로그인 할 수 없습니다입니다.누구에게도 해결책이 있습니까?
왜 내가 Role 클래스 – veer7