【问题标题】:how to authenticate user using spring security?如何使用 Spring Security 对用户进行身份验证?
【发布时间】:2014-04-22 03:20:17
【问题描述】:

我想实现 spring 安全登录和注销,所以我尝试了以下操作, Spring-context.xml:

<http auto-config="true">
        <intercept-url pattern="/**" access="isAuthenticated"/>  <!-- this  means all URL in this app will be checked if user is authenticated -->
        <form-login/> <!--  -->
        <logout logout-url="/logout" logout-success-url=""/>
 </http>
 <authentication-manager>
        <authentication-provider> 
            <user-service>
                <user name="sachin" password="sachin123" authorities="Admin"/>
            </user-service>
 </authentication-provider>

spring-servlet.xml:

<context:component-scan base-package="com.hrportal.controller" />
    </context:annotation-config>
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>

    <bean id="localeChangeInterceptor"
        class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang" />
    </bean>

    <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="en" />
    </bean>

    <bean id="handlerMapping"
        class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="interceptors">
            <ref bean="localeChangeInterceptor" />
        </property>
            </bean>
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
    <mvc:resources mapping="/images/**"  location="/images/"/>
    <mvc:resources location="/css/**" mapping="/css/"/>
    <mvc:resources location="/css/**" mapping="/css/"/>
    <mvc:default-servlet-handler/>

我正在手动进行数据库连接和访问,这就是为什么我无法使用 spring security 实现登录和注销。

我正在使用以下类手动连接数据库:

private static Connection con = null;

public static Connection getConnection() {
    try {
        if (con == null) {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/portal",
                    "root", "root");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return con;
}

}

谁能解释我解决这个问题..

【问题讨论】:

  • 你能解释一下是什么问题吗?
  • @geoand,其实我是 Spring 技术的新手.....而且 sevlet 非常好,我想使用日志和注销服务,所以我遵循了一些在线教程,但无法完全理解。
  • 我正在手动对用户进行身份验证,所以有什么办法可以做到这一点吗?
  • 看看这个stackoverflow.com/questions/18220556/…。它可能符合您的需求

标签: java spring spring-mvc spring-security


【解决方案1】:

尝试使用 spring roo,这个 rad 工具可以帮助您自动生成代码和配置 spring 安全性。 http://docs.spring.io/spring-roo/reference/html/beginning.html

【讨论】:

    猜你喜欢
    • 2020-04-16
    • 2011-08-12
    • 2011-08-21
    • 2012-11-27
    • 2019-12-26
    • 2017-05-18
    • 1970-01-01
    • 2017-12-21
    相关资源
    最近更新 更多