简体   繁体   English

如何使用Spring Security验证用户身份?

[英]how to authenticate user using spring security?

I wants to implement spring security log in and log out so I have tried following things , Spring-context.xml: 我想实现Spring Security登录和注销,所以我尝试了以下操作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: 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/>

I am doing database connection and access manually that is why I am not able to implement the log in and log out using spring security. 我正在手动进行数据库连接和访问,这就是为什么我无法使用Spring Security实现登录和注销的原因。

I am using the following class to connect with database manually : 我正在使用以下类手动连接数据库:

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;
}

}

can anyone explain me to solve this.. 谁能解释我解决这个问题。

Try using spring roo, this rad tool help you to generate code and configure spring security automatically. 尝试使用spring roo,此rad工具可帮助您生成代码并自动配置spring安全性。 http://docs.spring.io/spring-roo/reference/html/beginning.html http://docs.spring.io/spring-roo/reference/html/beginning.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用DaoAuthenticationProvider以编程方式使用Spring Security对用户进行身份验证 - How can I programmatically authenticate user with Spring Security using DaoAuthenticationProvider Spring Security无法验证用户 - Spring security not authenticate the user Spring 安全性 - 使用用户名验证用户 - Spring Security - Authenticate user with username Java:用户使用带有Spring Security的Jersey Jax-RS进行身份验证 - Java: User Authenticate Using Jersey Jax-RS with Spring Security 使用Spring Security 3对仅具有用户名的用户进行REST身份验证 - Using Spring security 3 to authenticate against REST a user only with username 使用Spring Security Java在查看页面之前限制用户进行身份验证 - restrict user to authenticate before viewing pages using spring security java 如何使用Spring Security以编程方式验证`User`并使用我的`UserDetailsS​​ervie`实现? - How to programmatically authenticate `User` with spring security and use my `UserDetailsServie` implementation? 如何使用spring Security通过基于邮件和uid的LDAP对用户进行身份验证? - How to authenticate a user from LDAP based on mail and by uid with spring Security? 如何在spring security中只通过ip地址验证用户? - How can authenticate user by only ip address in spring security? 如何在spring security中成功注册后自动验证用户身份 - How to automatically authenticate user after successful registration in spring security
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM