2013-06-10 3 views
0

몇 가지 예제를 검색했지만 어떤 예제도 찾을 수 없습니다. loginController.login() 메소드에서해야 할 일. 스프링 보안에 사용자 이름과 암호 텍스트를 어떻게 전달할 수 있습니까?스프링 보안 로그인 컨트롤러 메서드

봄-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.2.xsd 
http://www.springframework.org/schema/security 
http://www.springframework.org/schema/security/spring-security-3.1.xsd"> 

<http auto-config="true" use-expressions="true"> 
    <intercept-url pattern="/pages/login.xhtml*" access="permitAll"/> 
    <intercept-url pattern="/**" access="hasRole('admin')" /> 
    <form-login login-page='/pages/login.xhtml' default-target-url="/pages/index.xhtml" 
       authentication-failure-url="/pages/login.xhtml"/> 
    <logout logout-success-url="/pages/logout.xhtml" /> 

</http> 
<!--Authentication Manager Details -->  
<authentication-manager alias="authenticationManager"> 
    <authentication-provider user-service-ref="customUserDetailsService"> 
     <password-encoder hash="md5"/> 
    </authentication-provider> 
</authentication-manager> 

내보기 login.xhtml

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:p="http://primefaces.org/ui"> 
<h:head> 
    <title>User Login</title>   
</h:head> 
<h:body> 
    <h:form id="loginFormId"> 
     <p:outputPanel id="loginOutputPanelId"> 
      <p:panelGrid id="loginInformationPanel" columns="2"> 
       <h:outputText value="Username: "/> 
       <p:inputText value="#{loginController.userName}"/> 
       <h:outputText value="Password: "/> 
       <p:inputText value="#{loginController.password}"/> 
      </p:panelGrid> 
      <p:commandButton value="Login" actionListener="#{loginController.login()}"/> 
     </p:outputPanel> 
    </h:form> 
</h:body> 
개인적으로

답변

1

나는 봄 - 보안 컨텍스트 값을 설정하려면이 방법을 사용

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; 
import org.springframework.security.core.Authentication; 
import org.springframework.security.core.context.SecurityContextHolder; 

//.... 

//In your login method: 
List<Authority> auths = new ArrayList<Authority>(); 
auths.add(new Authority("ROLE_USER")); //Role here, like "admin" 
Authentication authentication = new UsernamePasswordAuthenticationToken(token, null, auths); 
SecurityContextHolder.getContext().setAuthentication(authentication); 

Authority 클래스는 다음과 같다 :

import org.springframework.security.core.GrantedAuthority; 


public class Authority implements GrantedAuthority{ 
    private static final long serialVersionUID = 9170140593525051237L; 

    private String authority; 

    public Authority(String authority) { 
    super(); 
    this.authority = authority; 
    } 

    @Override 
    public String getAuthority() { 
    return authority; 
    } 
    @Override 
    public String toString() { 
    return "Authority [authority=" + authority + "]"; 
    } 

} 

나는이 다음과 같이 사용자 정보를 얻을 수

0

할 수 있습니다 당신 Pricipal 객체 도움이되기를 바랍니다.

login(Principal principal){ 

if (principal instanceof UsernamePasswordAuthenticationToken) { 
     UsernamePasswordAuthenticationToken userDetails =  (UsernamePasswordAuthenticationToken) principal; 
userDetails.getName(); 

} 
관련 문제